home *** CD-ROM | disk | FTP | other *** search
/ PC Open 107 / PC Open 107 CD 1.bin / CD1 / INTERNET / COPIA SITI / HTTrack / httrack.exe / {app} / src / htscoremain.c < prev    next >
Encoding:
C/C++ Source or Header  |  2004-10-05  |  92.8 KB  |  2,446 lines

  1. /* ------------------------------------------------------------ */
  2. /*
  3. HTTrack Website Copier, Offline Browser for Windows and Unix
  4. Copyright (C) Xavier Roche and other contributors
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19.  
  20.  
  21. Important notes:
  22.  
  23. - We hereby ask people using this source NOT to use it in purpose of grabbing
  24. emails addresses, or collecting any other private information on persons.
  25. This would disgrace our work, and spoil the many hours we spent on it.
  26.  
  27.  
  28. Please visit our Website: http://www.httrack.com
  29. */
  30.  
  31.  
  32. /* ------------------------------------------------------------ */
  33. /* File: httrack.c subroutines:                                 */
  34. /*       main routine (first called)                            */
  35. /* Author: Xavier Roche                                         */
  36. /* ------------------------------------------------------------ */
  37.  
  38. /* Internal engine bytecode */
  39. #define HTS_INTERNAL_BYTECODE
  40.  
  41. #include "htscoremain.h"
  42.  
  43. #include "htsglobal.h"
  44. #include "htscore.h"
  45. #include "htsdefines.h"
  46. #include "htsalias.h"
  47. #include "htswrap.h"
  48. #include "htsmodules.h"
  49. #include "htszlib.h"
  50.  
  51. #include <ctype.h>
  52. #if HTS_WIN
  53. #else
  54. #ifndef HTS_DO_NOT_USE_UID
  55. /* setuid */
  56. #include <pwd.h>
  57. #ifdef HAVE_UNISTD_H
  58. #include <unistd.h>
  59. #endif
  60. #endif
  61. #endif
  62.  
  63. extern int exit_xh;          // sortir prΘmaturΘment
  64.  
  65. /* Resolver */
  66. extern int IPV6_resolver;
  67.  
  68.  
  69. // Add a command in the argc/argv
  70. #define cmdl_add(token,argc,argv,buff,ptr) \
  71.   argv[argc]=(buff+ptr); \
  72.   strcpybuff(argv[argc],token); \
  73.   ptr += (strlen(argv[argc])+2); \
  74.   argc++
  75.  
  76. // Insert a command in the argc/argv
  77. #define cmdl_ins(token,argc,argv,buff,ptr) \
  78.   { \
  79.   int i; \
  80.   for(i=argc;i>0;i--)\
  81.   argv[i]=argv[i-1];\
  82.   } \
  83.   argv[0]=(buff+ptr); \
  84.   strcpybuff(argv[0],token); \
  85.   ptr += (strlen(argv[0])+2); \
  86.   argc++
  87.  
  88. #define htsmain_free() do { if (url != NULL) { free(url); } } while(0)
  89.  
  90. #define ensureUrlCapacity(url, urlsize, size) do { \
  91.   if (urlsize < size || url == NULL) { \
  92.     urlsize = size; \
  93.     if (url == NULL) { \
  94.       url = (char*) malloct(urlsize); \
  95.       if (url != NULL) url[0]='\0'; \
  96.     } else { \
  97.       url = (char*) realloct(url, urlsize); \
  98.     } \
  99.     if (url == NULL) { \
  100.       HTS_PANIC_PRINTF("* memory exhausted"); \
  101.       htsmain_free(); \
  102.       return -1; \
  103.     } \
  104.   } \
  105. } while(0)
  106.  
  107. void set_wrappers(void) {
  108. #if HTS_ANALYSTE
  109.   // custom wrappers
  110.   hts_htmlcheck_init         = (t_hts_htmlcheck_init)           htswrap_read("init");
  111.   hts_htmlcheck_uninit       = (t_hts_htmlcheck_uninit)         htswrap_read("free");
  112.   hts_htmlcheck_start        = (t_hts_htmlcheck_start)          htswrap_read("start");
  113.   hts_htmlcheck_end          = (t_hts_htmlcheck_end)            htswrap_read("end");
  114.   hts_htmlcheck_chopt        = (t_hts_htmlcheck_chopt)          htswrap_read("change-options");
  115.   hts_htmlcheck_preprocess   = (t_hts_htmlcheck_process)        htswrap_read("preprocess-html");
  116.   hts_htmlcheck_postprocess  = (t_hts_htmlcheck_process)        htswrap_read("postprocess-html");
  117.   hts_htmlcheck              = (t_hts_htmlcheck)                htswrap_read("check-html");
  118.   hts_htmlcheck_query        = (t_hts_htmlcheck_query)          htswrap_read("query");
  119.   hts_htmlcheck_query2       = (t_hts_htmlcheck_query2)         htswrap_read("query2");
  120.   hts_htmlcheck_query3       = (t_hts_htmlcheck_query3)         htswrap_read("query3");
  121.   hts_htmlcheck_loop         = (t_hts_htmlcheck_loop)           htswrap_read("loop");
  122.   hts_htmlcheck_check        = (t_hts_htmlcheck_check)          htswrap_read("check-link");
  123.   hts_htmlcheck_pause        = (t_hts_htmlcheck_pause)          htswrap_read("pause");
  124.   hts_htmlcheck_filesave     = (t_hts_htmlcheck_filesave)       htswrap_read("save-file");
  125.   hts_htmlcheck_linkdetected = (t_hts_htmlcheck_linkdetected)   htswrap_read("link-detected");
  126.   hts_htmlcheck_linkdetected2 = (t_hts_htmlcheck_linkdetected2) htswrap_read("link-detected2");
  127.   hts_htmlcheck_xfrstatus    = (t_hts_htmlcheck_xfrstatus)      htswrap_read("transfer-status");
  128.   hts_htmlcheck_savename     = (t_hts_htmlcheck_savename)       htswrap_read("save-name");
  129.   hts_htmlcheck_sendhead     = (t_hts_htmlcheck_sendhead)       htswrap_read("send-header");
  130.   hts_htmlcheck_receivehead  = (t_hts_htmlcheck_receivehead)    htswrap_read("receive-header");
  131. #endif
  132. }
  133.  
  134. // Main, rΘcupΦre les paramΦtres et appelle le robot
  135. #if HTS_ANALYSTE
  136. HTSEXT_API int hts_main(int argc, char **argv) {
  137. #else
  138. int main(int argc, char **argv) {
  139. #endif
  140.   char** x_argv=NULL;     // Patch pour argv et argc: en cas de rΘcupΘration de ligne de commande
  141.   char* x_argvblk=NULL;   // (reprise ou update)
  142.   int   x_ptr=0;          // offset
  143.   //
  144.   int argv_url=-1;           // ==0 : utiliser cache et doit.log
  145.   char* argv_firsturl=NULL;  // utilisΘ pour nommage par dΘfaut
  146.   char* url = NULL;          // URLS sΘparΘes par un espace
  147.   int   url_sz = 65535;
  148.   //char url[65536];         // URLS sΘparΘes par un espace
  149.   // the parametres
  150.   httrackp BIGSTK httrack;
  151.   int httrack_logmode=3;   // ONE log file
  152.   int recuperer=0;         // rΘcupΘrer un plantage (n'arrive jamais, α supprimer)
  153. #if HTS_WIN
  154. #if HTS_ANALYSTE!=2
  155.   WORD   wVersionRequested; /* requested version WinSock API */ 
  156.   WSADATA BIGSTK wsadata;   /* Windows Sockets API data */
  157. #endif
  158. #else
  159. #ifndef HTS_DO_NOT_USE_UID
  160.   int switch_uid=-1,switch_gid=-1;      /* setuid/setgid */
  161. #endif
  162.   int switch_chroot=0;                  /* chroot ? */
  163. #endif
  164.   //
  165.   ensureUrlCapacity(url, url_sz, 65536);
  166.   //
  167.  
  168. #if HTS_ANALYSTE
  169.   // custom wrappers
  170.   set_wrappers();
  171. #endif
  172.  
  173.   // options par dΘfaut
  174.   memset(&httrack, 0, sizeof(httrackp));
  175.   httrack.wizard=2;   // wizard automatique
  176.   httrack.quiet=0;     // questions
  177.   //  
  178.   httrack.travel=0;   // mΩme adresse
  179.   httrack.depth=9999; // mirror total par dΘfaut
  180.   httrack.extdepth=0; // mais pas α l'extΘrieur
  181.   httrack.seeker=1;   // down 
  182.   httrack.urlmode=2;  // relatif par dΘfaut
  183.   httrack.debug=0;    // pas de dΘbug en plus
  184.   httrack.getmode=3;  // linear scan
  185.   httrack.maxsite=-1; // taille max site (aucune)
  186.   httrack.maxfile_nonhtml=-1; // taille max fichier non html
  187.   httrack.maxfile_html=-1;    // idem pour html
  188.   httrack.maxsoc=4;     // nbre socket max
  189.   httrack.fragment=-1;  // pas de fragmentation
  190.   httrack.nearlink=0;   // ne pas prendre les liens non-html "adjacents"
  191.   httrack.makeindex=1;  // faire un index
  192.   httrack.kindex=0;     // index 'keyword'
  193.   httrack.delete_old=1; // effacer anciens fichiers
  194.   httrack.makestat=0;  // pas de fichier de stats
  195.   httrack.maketrack=0; // ni de tracking
  196.   httrack.timeout=120; // timeout par dΘfaut (2 minutes)
  197.   httrack.cache=1;     // cache prioritaire
  198.   httrack.shell=0;     // pas de shell par defaut
  199.   httrack.proxy.active=0;    // pas de proxy
  200.   strcpybuff(httrack.proxy.bindhost, "");  // bind default host
  201.   httrack.user_agent_send=1; // envoyer un user-agent
  202.   strcpybuff(httrack.user_agent,"Mozilla/4.5 (compatible; HTTrack 3.0x; Windows 98)");
  203.   strcpybuff(httrack.referer, "");
  204.   strcpybuff(httrack.from, "");
  205.   httrack.savename_83=0;     // noms longs par dΘfaut
  206.   httrack.savename_type=0;   // avec structure originale
  207.   httrack.mimehtml=0;        // pas MIME-html
  208.   httrack.parsejava=1;       // parser classes
  209.   httrack.hostcontrol=0;     // PAS de control host pour timeout et traffic jammer
  210.   httrack.retry=2;           // 2 retry par dΘfaut
  211.   httrack.errpage=1;         // copier ou gΘnΘrer une page d'erreur en cas d'erreur (404 etc.)
  212.   httrack.check_type=1;      // vΘrifier type si inconnu (cgi,asp..) SAUF / considΘrΘ comme html
  213.   httrack.all_in_cache=0;    // ne pas tout stocker en cache
  214.   httrack.robots=2;          // traiter les robots.txt
  215.   httrack.external=0;        // liens externes normaux
  216.   httrack.passprivacy=0;     // mots de passe dans les fichiers
  217.   httrack.includequery=1;    // include query-string par dΘfaut
  218.   httrack.mirror_first_page=0;  // pas mode mirror links
  219.   httrack.accept_cookie=1;   // gΘrer les cookies
  220.   httrack.cookie=NULL;
  221.   httrack.http10=0;          // laisser http/1.1
  222.   httrack.nokeepalive = 0;   // pas keep-alive
  223.   httrack.nocompression=0;   // pas de compression
  224.   httrack.tolerant=0;        // ne pas accepter content-length incorrect
  225.   httrack.parseall=1;        // tout parser (tags inconnus, par exemple)
  226.   httrack.parsedebug=0;      // pas de mode dΘbuggage
  227.   httrack.norecatch=0;       // ne pas reprendre les fichiers effacΘs par l'utilisateur
  228.   httrack.verbosedisplay=0;  // pas d'animation texte
  229.   httrack.sizehack=0;        // size hack
  230.   httrack.urlhack=1;         // url hack (normalizer)
  231.   strcpybuff(httrack.footer,HTS_DEFAULT_FOOTER);
  232.   httrack.ftp_proxy=1;       // proxy http pour ftp
  233.   strcpybuff(httrack.filelist,"");
  234.   strcpybuff(httrack.lang_iso,"en, *");
  235.   strcpybuff(httrack.mimedefs,"\n"); // aucun filtre mime (\n IMPORTANT)
  236.   //
  237.   httrack.log=stdout;
  238.   httrack.errlog=stderr;
  239.   httrack.flush=1;           // flush sur les fichiers log
  240.   //httrack.aff_progress=0;
  241.   httrack.keyboard=0;
  242.   //
  243.   strcpybuff(httrack.path_html,"");
  244.   strcpybuff(httrack.path_log,"");
  245.   strcpybuff(httrack.path_bin,"");
  246.   //
  247. #if HTS_SPARE_MEMORY==0
  248.   httrack.maxlink=100000;    // 100,000 liens max par dΘfaut (400Kb)
  249.   httrack.maxfilter=200;     // 200 filtres max par dΘfaut
  250. #else
  251.   httrack.maxlink=10000;     // 10,000 liens max par dΘfaut (40Kb)
  252.   httrack.maxfilter=50;      // 50 filtres max par dΘfaut
  253. #endif
  254.   httrack.maxcache=1048576*32;  // a peu prΦs 32Mo en cache max -- OPTION NON PARAMETRABLE POUR L'INSTANT --
  255.   //httrack.maxcache_anticipate=256;  // maximum de liens α anticiper
  256.   httrack.maxtime=-1;        // temps max en secondes
  257.   httrack.maxrate=-1;        // pas de taux maxi
  258.   httrack.maxconn=10.0;      // nombre connexions/s
  259.   httrack.waittime=-1;       // wait until.. hh*3600+mm*60+ss
  260.   //
  261.   httrack.exec=argv[0];
  262.   httrack.is_update=0;      // not an update (yet)
  263.   httrack.dir_topindex=0;   // do not built top index (yet)
  264.   //
  265.   httrack.state.stop=0;     // stopper
  266.   httrack.state.exit_xh=0;  // abort
  267.   //
  268.   _DEBUG_HEAD=0;            // pas de debuggage en tΩtes
  269.  
  270.   
  271. #if HTS_WIN
  272. #if HTS_ANALYSTE!=2
  273.   {
  274.     int stat;
  275.     wVersionRequested = 0x0101;
  276.     stat = WSAStartup( wVersionRequested, &wsadata );
  277.     if (stat != 0) {
  278.       HTS_PANIC_PRINTF("Winsock not found!\n");
  279.       htsmain_free();
  280.       return -1;
  281.     } else if (LOBYTE(wsadata.wVersion) != 1  && HIBYTE(wsadata.wVersion) != 1) {
  282.       HTS_PANIC_PRINTF("WINSOCK.DLL does not support version 1.1\n");
  283.       WSACleanup();
  284.       htsmain_free();
  285.       return -1;
  286.     }
  287.   }
  288. #endif
  289. #endif
  290.  
  291.   /* Init root dir */
  292.   hts_rootdir(argv[0]);
  293.  
  294. #if HTS_WIN
  295. #else
  296.   /* Terminal is a tty, may ask questions and display funny information */
  297.   if (isatty(1)) {
  298.     httrack.quiet=0;
  299.     httrack.verbosedisplay=1;
  300.   }
  301.   /* Not a tty, no stdin input or funny output! */
  302.   else {
  303.     httrack.quiet=1;
  304.     httrack.verbosedisplay=0;
  305.   }
  306. #endif
  307.  
  308.   /* First test: if -#R then only launch ftp */
  309.   if (argc > 2) {
  310.     if (strcmp(argv[1],"-#R")==0) {
  311.       if (argc==6) {
  312.         lien_back r;
  313.         char* path;
  314.         FILE* fp;
  315.         strcpybuff(r.url_adr,argv[2]);
  316.         strcpybuff(r.url_fil,argv[3]);
  317.         strcpybuff(r.url_sav,argv[4]);
  318.         path=argv[5];
  319.         r.status=1000;
  320.         run_launch_ftp(&r);
  321.         fp=fopen(fconv(path),"wb");
  322.         if (fp) {
  323.           fprintf(fp,"%d %s",r.r.statuscode,r.r.msg);
  324.           fclose(fp); fp=NULL;
  325.           rename(fconv(path),fconcat(path,".ok"));
  326.         } else remove(fconv(path));
  327.       } else {
  328.         printf("htsftp error, wrong parameter number (%d)\n",argc);
  329.       }
  330.       exit(0);   // pas _exit()
  331.     }
  332.   }
  333.  
  334.   // ok, non ftp, continuer
  335.  
  336.  
  337.   // Binary program path?
  338. #ifndef HTS_HTTRACKDIR
  339.   {
  340.     char* path=fslash(argv[0]);
  341.     char* a;
  342.     if ((a=strrchr(path,'/'))) {
  343.       httrack.path_bin[0]='\0';
  344.       strncatbuff(httrack.path_bin,argv[0],(int) a - (int) path);
  345.     }
  346.   }
  347. #else
  348.   strcpybuff(httrack.path_bin, HTS_HTTRACKDIR);
  349. #endif
  350.  
  351.   /* libhttrack-plugin DLL preload (libhttrack-plugin.so or libhttrack-plugin.dll) */
  352.   {
  353.     void* userfunction = getFunctionPtr(&httrack, "libhttrack-plugin", "plugin_init");
  354.     if (userfunction != NULL) {
  355.       t_hts_htmlcheck_init initFnc = (t_hts_htmlcheck_init) userfunction;
  356.       initFnc();
  357.       set_wrappers();        /* Re-read wrappers internal static functions */
  358.     }
  359.   }
  360.  
  361.   /* filter CR, LF, TAB.. */
  362.   {
  363.     int na;
  364.     for(na=1;na<argc;na++) {
  365.       char* a;
  366.       while( (a=strchr(argv[na],'\x0d')) ) *a=' ';
  367.       while( (a=strchr(argv[na],'\x0a')) ) *a=' ';
  368.       while( (a=strchr(argv[na],9)) )      *a=' ';
  369.       /* equivalent to "empty parameter" */
  370.       if ((strcmp(argv[na],HTS_NOPARAM)==0) || (strcmp(argv[na],HTS_NOPARAM2)==0))        // (none)
  371.         strcpybuff(argv[na],"\"\"");
  372.       if (strncmp(argv[na],"-&",2)==0)
  373.         argv[na][1]='%';
  374.     }
  375.   }
  376.  
  377.  
  378.  
  379.   /* create x_argvblk buffer for transformed command line */
  380.   {
  381.     int current_size=0;
  382.     int size;
  383.     int na;
  384.     for(na=0;na<argc;na++)
  385.       current_size += (strlen(argv[na]) + 1);
  386.     if ((size=fsize("config"))>0)
  387.       current_size += size;
  388.     x_argvblk=(char*) malloct(current_size+32768);
  389.     if (x_argvblk == NULL) {
  390.       HTS_PANIC_PRINTF("Error, not enough memory");
  391.       htsmain_free();
  392.       return -1;
  393.     }
  394.     x_argvblk[0]='\0';
  395.     x_ptr=0;
  396.  
  397.     /* Create argv */
  398.     x_argv = (char**) malloct(sizeof(char*) * ( argc + 1024 ));
  399.   }
  400.  
  401.   /* Create new argc/argv, replace alias, count URLs, treat -h, -q, -i */
  402.   {
  403.     char BIGSTK _tmp_argv[2][HTS_CDLMAXSIZE];
  404.     char BIGSTK tmp_error[HTS_CDLMAXSIZE];
  405.     char* tmp_argv[2];
  406.     int tmp_argc;
  407.     int x_argc=0;
  408.     int na;
  409.     tmp_argv[0]=_tmp_argv[0];
  410.     tmp_argv[1]=_tmp_argv[1];
  411.     //
  412.     argv_url=0;       /* pour comptage */
  413.     //
  414.     cmdl_add(argv[0],x_argc,x_argv,x_argvblk,x_ptr);
  415.     na=1;             /* commencer aprΦs nom_prg */
  416.     while(na<argc) {
  417.       int result=1;
  418.       tmp_argv[0][0]=tmp_argv[1][0]='\0';
  419.  
  420.       /* VΘrifier argv[] non vide */
  421.       if (strnotempty(argv[na])) {
  422.         
  423.         /* VΘrifier Commande (alias) */
  424.         result=optalias_check(argc,(const char * const *)argv,na,
  425.           &tmp_argc,(char**)tmp_argv,tmp_error);
  426.         if (!result) {
  427.           HTS_PANIC_PRINTF(tmp_error);
  428.           htsmain_free();
  429.           return -1;
  430.         }
  431.         
  432.         /* Copier */
  433.         cmdl_add(tmp_argv[0],x_argc,x_argv,x_argvblk,x_ptr);
  434.         if (tmp_argc > 1) {
  435.           cmdl_add(tmp_argv[1],x_argc,x_argv,x_argvblk,x_ptr);
  436.         }
  437.         
  438.         /* Compter URLs et dΘtecter -i,-q.. */
  439.         if (tmp_argc == 1) {           /* pas -P & co */
  440.           if (!cmdl_opt(tmp_argv[0])) {   /* pas -c0 & co */
  441.             if (argv_url<0) argv_url=0;   // -1==force -> 1=one url already detected, wipe all previous options
  442.             //if (argv_url>=0) {
  443.             argv_url++;
  444.             if (!argv_firsturl)
  445.               argv_firsturl=x_argv[x_argc-1];
  446.             //}
  447.           } else {
  448.             if (strcmp(tmp_argv[0],"-h")==0) {
  449.               help(argv[0],!httrack.quiet);
  450.               htsmain_free();
  451.               return 0;
  452.             } else {
  453.               if (strncmp(tmp_argv[0],"--",2)) {   /* pas */
  454.                 if ((strchr(tmp_argv[0],'q')!=NULL))
  455.                   httrack.quiet=1;    // ne pas poser de questions! (nohup par exemple)
  456.                 if ((strchr(tmp_argv[0],'i')!=NULL)) {  // doit.log!
  457.                   argv_url=-1;        /* forcer */
  458.                   httrack.quiet=1;
  459.                 }
  460.               } else if (strcmp(tmp_argv[0] + 2,"quiet") == 0) {
  461.                 httrack.quiet=1;    // ne pas poser de questions! (nohup par exemple)
  462.               } else if (strcmp(tmp_argv[0] + 2,"continue") == 0) {
  463.                 argv_url=-1;        /* forcer */
  464.                 httrack.quiet=1;
  465.               }
  466.             }
  467.           }
  468.         } else if (tmp_argc == 2) {
  469.           if ((strcmp(tmp_argv[0],"-%L")==0)) {  // liste d'URLs
  470.             if (argv_url<0) argv_url=0;   // -1==force -> 1=one url already detected, wipe all previous options
  471.             //if (argv_url>=0)
  472.             argv_url++;        /* forcer */
  473.           }
  474.         }
  475.       }
  476.  
  477.       na+=result;
  478.     }
  479.     if (argv_url<0)
  480.       argv_url=0;
  481.  
  482.     /* Nouveaux argc et argv */
  483.     argv=x_argv;
  484.     argc=x_argc;
  485.   }
  486.  
  487.   // Option O and includerc
  488.   { 
  489.     int loops=0;
  490.     while (loops<2) {
  491.       char* com;
  492.       int na;
  493.       
  494.       for(na=1;na<argc;na++) {
  495.         
  496.         if (argv[na][0]=='"') {
  497.           char BIGSTK tempo[HTS_CDLMAXSIZE];
  498.           strcpybuff(tempo,argv[na]+1);
  499.           if (tempo[strlen(tempo)-1]!='"') {
  500.             char BIGSTK s[HTS_CDLMAXSIZE];
  501.             sprintf(s,"Missing quote in %s",argv[na]);
  502.             HTS_PANIC_PRINTF(s);
  503.             htsmain_free();
  504.             return -1;
  505.           }
  506.           tempo[strlen(tempo)-1]='\0';
  507.           strcpybuff(argv[na],tempo);
  508.         }
  509.         
  510.         if (cmdl_opt(argv[na])) { // option
  511.           com=argv[na]+1;
  512.           
  513.           while(*com) {
  514.             switch(*com) {
  515.             case 'O':    // output path
  516.               if ((na+1>=argc) || (argv[na+1][0]=='-')) {
  517.                 HTS_PANIC_PRINTF("Option O needs to be followed by a blank space, and a path (or path,path)");
  518.                 printf("Example: -O /binary/\n");
  519.                 printf("Example: -O /binary/,/log/\n");
  520.                 htsmain_free();
  521.                 return -1;
  522.               } else {
  523.                 char* a;
  524.                 na++;
  525.                 strcpybuff(httrack.path_html,"");
  526.                 strcpybuff(httrack.path_log,"");
  527.                 a=strstr(argv[na],"\",\"");  // rechercher en premier, au cas ou -O "c:\pipo,test","c:\test"
  528.                 if (!a)
  529.                   a=strchr(argv[na],',');  // 2 path
  530.                 else
  531.                   a++;  // position ,
  532.                 if (a) {
  533.                   strncatbuff(httrack.path_html,argv[na],(int) (a-argv[na]));
  534.                   strcatbuff(httrack.path_log,a+1);
  535.                 } else {
  536.                   strcpybuff(httrack.path_log,argv[na]);
  537.                   strcpybuff(httrack.path_html,argv[na]);
  538.                 }
  539.                 // Eliminer les cas comme -O "C:\mirror\"
  540.                 if (httrack.path_log[0]=='"') {  // Guillemets
  541.                   char tmp[256];
  542.                   strcpybuff(tmp,httrack.path_log+1);
  543.                   if (tmp[strlen(tmp)-1]=='"')
  544.                     tmp[strlen(tmp)-1]='\0';
  545.                   strcpybuff(httrack.path_log,tmp);
  546.                 }
  547.                 if (httrack.path_html[0]=='"') {
  548.                   char tmp[256];
  549.                   strcpybuff(tmp,httrack.path_html+1);
  550.                   if (tmp[strlen(tmp)-1]=='"')
  551.                     tmp[strlen(tmp)-1]='\0';
  552.                   strcpybuff(httrack.path_html,tmp);
  553.                 }
  554.                 check_path(httrack.path_log,argv_firsturl);
  555.                 if (check_path(httrack.path_html,argv_firsturl)) {
  556.                   httrack.dir_topindex=1;     // rebuilt top index
  557.                 }
  558.                 
  559.                 //printf("-->%s\n%s\n",httrack.path_html,httrack.path_log);
  560.                 
  561.               }
  562.               break;
  563.             }  // switch
  564.             com++;    
  565.           }  // while
  566.           
  567.         }  // arg
  568.         
  569.       }  // for
  570.      
  571.          /* if doit.log exists, or if new URL(s) defined, 
  572.       then DO NOT load standard config files */
  573.       /* (config files are added in doit.log) */
  574. #if DEBUG_STEPS
  575.       printf("Loading httrackrc/doit.log\n");
  576. #endif
  577.       /* recreate a doit.log (no old doit.log or new URLs (and parameters)) */
  578.       if ((strnotempty(httrack.path_log)) || (strnotempty(httrack.path_html)))
  579.         loops++;      // do not loop once again and do not include rc file (O option exists)
  580.       else {
  581.         if ( (!fexist(fconcat(httrack.path_log,"hts-cache/doit.log"))) || (argv_url>0) ) {
  582.           if (!optinclude_file(fconcat(httrack.path_log,HTS_HTTRACKRC),&argc,argv,x_argvblk,&x_ptr))
  583.             if (!optinclude_file(HTS_HTTRACKRC,&argc,argv,x_argvblk,&x_ptr)) {
  584.               if (!optinclude_file(fconcat(hts_gethome(),"/"HTS_HTTRACKRC),&argc,argv,x_argvblk,&x_ptr)) {
  585. #ifdef HTS_HTTRACKCNF
  586.                 optinclude_file(HTS_HTTRACKCNF,&argc,argv,x_argvblk,&x_ptr);
  587. #endif
  588.               }
  589.             }
  590.         } else
  591.           loops++;      // do not loop once again
  592.       }
  593.  
  594.       loops++;
  595.    } // while
  596.  
  597.   }  // traiter -O
  598.  
  599.   /* load doit.log and insert in current command line */
  600.   if ( fexist(fconcat(httrack.path_log,"hts-cache/doit.log")) && (argv_url<=0) ) {
  601.     FILE* fp=fopen(fconcat(httrack.path_log,"hts-cache/doit.log"),"rb");
  602.     if (fp) {
  603.       int insert_after=1;     /* insΘrer aprΦs nom au dΘbut */
  604.       //
  605.       char BIGSTK buff[8192];
  606.       char *p,*lastp;
  607.       linput(fp,buff,8000);
  608.       fclose(fp); fp=NULL;
  609.       p=buff;
  610.       do {
  611.         int insert_after_argc;
  612.         // read next
  613.         lastp=p;
  614.         if (p) {
  615.           p=next_token(p,1);
  616.           if (p) {
  617.             *p=0;    // null
  618.             p++;
  619.           }
  620.         }
  621.  
  622.         /* Insert parameters BUT so that they can be in the same order */
  623.         if (lastp) {
  624.           if (strnotempty(lastp)) {
  625.             insert_after_argc=argc-insert_after;
  626.             cmdl_ins(lastp,insert_after_argc,(argv+insert_after),x_argvblk,x_ptr);
  627.             argc=insert_after_argc+insert_after;
  628.             insert_after++;
  629.           }
  630.         }
  631.       } while(lastp!=NULL);
  632.       //fclose(fp);
  633.     }
  634.   }
  635.  
  636.  
  637.   // Existence d'un cache - pas de new mais un old.. renommer
  638. #if DEBUG_STEPS
  639.   printf("Checking cache\n");
  640. #endif
  641.   if (!fexist(fconcat(httrack.path_log,"hts-cache/new.zip"))) {
  642.     if ( fexist(fconcat(httrack.path_log,"hts-cache/old.zip")) ) {
  643.       rename(fconcat(httrack.path_log,"hts-cache/old.zip"),fconcat(httrack.path_log,"hts-cache/new.zip"));
  644.     }
  645.   } else if ( (!fexist(fconcat(httrack.path_log,"hts-cache/new.dat"))) || (!fexist(fconcat(httrack.path_log,"hts-cache/new.ndx"))) ) {
  646.     if ( (fexist(fconcat(httrack.path_log,"hts-cache/old.dat"))) && (fexist(fconcat(httrack.path_log,"hts-cache/old.ndx"))) ) {
  647.       remove(fconcat(httrack.path_log,"hts-cache/new.dat"));
  648.       remove(fconcat(httrack.path_log,"hts-cache/new.ndx"));
  649.       //remove(fconcat(httrack.path_log,"hts-cache/new.lst"));
  650.       rename(fconcat(httrack.path_log,"hts-cache/old.dat"),fconcat(httrack.path_log,"hts-cache/new.dat"));
  651.       rename(fconcat(httrack.path_log,"hts-cache/old.ndx"),fconcat(httrack.path_log,"hts-cache/new.ndx"));
  652.       //rename(fconcat(httrack.path_log,"hts-cache/old.lst"),fconcat(httrack.path_log,"hts-cache/new.lst"));
  653.     }
  654.   }
  655.  
  656.   /* Interrupted mirror detected */
  657.   if (!httrack.quiet) {
  658.     if (fexist(fconcat(httrack.path_log,"hts-in_progress.lock"))) {
  659.       /* Old cache */
  660.       if ( (fexist(fconcat(httrack.path_log,"hts-cache/old.dat"))) && (fexist(fconcat(httrack.path_log,"hts-cache/old.ndx"))) ) {
  661.         if (httrack.log != NULL) {
  662.           fprintf(httrack.log,"Warning!\n");
  663.           fprintf(httrack.log,"An aborted mirror has been detected!\nThe current temporary cache is required for any update operation and only contains data downloaded during the last aborted session.\nThe former cache might contain more complete information; if you do not want to lose that information, you have to restore it and delete the current cache.\nThis can easily be done here by erasing the hts-cache/new.* files\n");
  664.           fprintf(httrack.log,"Please restart HTTrack with --continue (-iC1) option to override this message!\n");
  665.         }
  666.         exit(0);
  667.       }
  668.     }
  669.   }
  670.     
  671.   // remplacer "macros" comme --spider
  672.   // permet de lancer httrack sans a avoir α se rappeler de syntaxes comme p0C0I0Qc32 ..
  673. #if DEBUG_STEPS
  674.   printf("Checking last macros\n");
  675. #endif
  676.   {
  677.     int i;
  678.     for(i=0;i<argc;i++) {
  679. #if DEBUG_STEPS
  680.       printf("Checking #%d:\n",argv[i]);
  681.       printf("%s\n",argv[i]);
  682. #endif
  683.       if (argv[i][0]=='-') {
  684.         if (argv[i][1]=='-') {  // --xxx
  685.           if ((strfield2(argv[i]+2,"clean")) || (strfield2(argv[i]+2,"tide"))) {  // nettoyer
  686.             strcpybuff(argv[i]+1,"");
  687.             if (fexist(fconcat(httrack.path_log,"hts-log.txt")))
  688.               remove(fconcat(httrack.path_log,"hts-log.txt"));
  689.             if (fexist(fconcat(httrack.path_log,"hts-err.txt")))
  690.               remove(fconcat(httrack.path_log,"hts-err.txt"));
  691.             if (fexist(fconcat(httrack.path_html,"index.html")))
  692.               remove(fconcat(httrack.path_html,"index.html"));
  693.             /* */
  694.             if (fexist(fconcat(httrack.path_log,"hts-cache/new.zip")))
  695.               remove(fconcat(httrack.path_log,"hts-cache/new.zip"));
  696.             if (fexist(fconcat(httrack.path_log,"hts-cache/old.zip")))
  697.               remove(fconcat(httrack.path_log,"hts-cache/old.zip"));
  698.             if (fexist(fconcat(httrack.path_log,"hts-cache/new.dat")))
  699.               remove(fconcat(httrack.path_log,"hts-cache/new.dat"));
  700.             if (fexist(fconcat(httrack.path_log,"hts-cache/new.ndx")))
  701.               remove(fconcat(httrack.path_log,"hts-cache/new.ndx"));
  702.             if (fexist(fconcat(httrack.path_log,"hts-cache/old.dat")))
  703.               remove(fconcat(httrack.path_log,"hts-cache/old.dat"));
  704.             if (fexist(fconcat(httrack.path_log,"hts-cache/old.ndx")))
  705.               remove(fconcat(httrack.path_log,"hts-cache/old.ndx"));
  706.             if (fexist(fconcat(httrack.path_log,"hts-cache/new.lst")))
  707.               remove(fconcat(httrack.path_log,"hts-cache/new.lst"));
  708.             if (fexist(fconcat(httrack.path_log,"hts-cache/old.lst")))
  709.               remove(fconcat(httrack.path_log,"hts-cache/old.lst"));
  710.             if (fexist(fconcat(httrack.path_log,"hts-cache/new.txt")))
  711.               remove(fconcat(httrack.path_log,"hts-cache/new.txt"));
  712.             if (fexist(fconcat(httrack.path_log,"hts-cache/old.txt")))
  713.               remove(fconcat(httrack.path_log,"hts-cache/old.txt"));
  714.             if (fexist(fconcat(httrack.path_log,"hts-cache/doit.log")))
  715.               remove(fconcat(httrack.path_log,"hts-cache/doit.log"));
  716.             if (fexist(fconcat(httrack.path_log,"hts-in_progress.lock")))
  717.               remove(fconcat(httrack.path_log,"hts-in_progress.lock"));
  718.             rmdir(fconcat(httrack.path_log,"hts-cache"));
  719.             //
  720.           } else if (strfield2(argv[i]+2,"catchurl")) {      // capture d'URL via proxy temporaire!
  721.             argv_url=1;     // forcer a passer les parametres
  722.             strcpybuff(argv[i]+1,"#P");
  723.             //
  724.           } else if (strfield2(argv[i]+2,"updatehttrack")) {
  725. #ifdef _WIN32
  726.             char s[HTS_CDLMAXSIZE];
  727.             sprintf(s,"%s not available in this version",argv[i]);
  728.             HTS_PANIC_PRINTF(s);
  729.             htsmain_free();
  730.             return -1;
  731. #else
  732. #if 0
  733.             char _args[8][256];
  734.             char *args[8];
  735.             
  736.             printf("Cheking for updates...\n");
  737.             strcpybuff(_args[0],argv[0]);
  738.             strcpybuff(_args[1],"--get");
  739.             sprintf(_args[2],HTS_UPDATE_WEBSITE,HTS_PLATFORM,"");
  740.             strcpybuff(_args[3],"--quickinfo");
  741.             args[0]=_args[0];
  742.             args[1]=_args[1];
  743.             args[2]=_args[2];
  744.             args[3]=_args[3];
  745.             args[4]=NULL;
  746.             if (execvp(args[0],args)==-1) {
  747.             }
  748. #endif
  749. #endif
  750.           }
  751.           //
  752.           else {
  753.             char s[HTS_CDLMAXSIZE];
  754.             sprintf(s,"%s not recognized",argv[i]);
  755.             HTS_PANIC_PRINTF(s);
  756.             htsmain_free();
  757.             return -1;
  758.           }
  759.  
  760.         } 
  761.       }
  762.     }
  763.   }
  764.  
  765.   // Compter urls/jokers
  766.   /*
  767.   if (argv_url<=0) { 
  768.     int na;
  769.     argv_url=0;
  770.     for(na=1;na<argc;na++) {
  771.       if ( (strcmp(argv[na],"-P")==0) || (strcmp(argv[na],"-N")==0) || (strcmp(argv[na],"-F")==0) || (strcmp(argv[na],"-O")==0) || (strcmp(argv[na],"-V")==0) ) {
  772.         na++;    // sauter nom de proxy
  773.       } else if (!cmdl_opt(argv[na])) { 
  774.         argv_url++;   // un de plus       
  775.       } else if (strcmp(argv[na],"-h")==0) {
  776.         help(argv[0],!httrack.quiet);
  777.         htsmain_free();
  778.         return 0;
  779.       } else {
  780.         if ((strchr(argv[na],'q')!=NULL))
  781.           httrack.quiet=1;    // ne pas poser de questions! (nohup par exemple)
  782.         if ((strchr(argv[na],'i')!=NULL)) {  // doit.log!
  783.           argv_url=0;
  784.           na=argc;
  785.         }
  786.       }
  787.     }
  788.   }  
  789.   */
  790.  
  791.   // Ici on ajoute les arguments qui ont ΘtΘ appelΘs avant au cas o∙ on rΘcupΦre une session
  792.   // Exemple: httrack www.truc.fr -L0 puis ^C puis httrack sans URL : ajouter URL prΘcΘdente
  793.   /*
  794.   if (argv_url==0) {
  795.     //if ((fexist(fconcat(httrack.path_log,"hts-cache/new.dat"))) && (fexist(fconcat(httrack.path_log,"hts-cache/new.ndx")))) {  // il existe dΘja un cache prΘcΘdent.. renommer
  796.     if (fexist(fconcat(httrack.path_log,"hts-cache/doit.log"))) {    // un cache est prΘsent
  797.       
  798.       x_argvblk=(char*) calloct(32768,1);
  799.       
  800.       if (x_argvblk!=NULL) {
  801.         FILE* fp;
  802.         int x_argc;
  803.         
  804.         //strcpybuff(x_argvblk,"httrack ");
  805.         fp=fopen(fconcat(httrack.path_log,"hts-cache/doit.log"),"rb");
  806.         if (fp) {
  807.           linput(fp,x_argvblk+strlen(x_argvblk),8192);
  808.           fclose(fp); fp=NULL;
  809.         }
  810.         
  811.         // calculer arguments selon derniers arguments
  812.         x_argv[0]=argv[0];
  813.         x_argc=1;
  814.         {
  815.           char* p=x_argvblk;
  816.           do {
  817.             x_argv[x_argc++]=p;
  818.             //p=strstr(p," ");
  819.             // exemple de chaine: "echo \"test\"" c:\a "\$0"
  820.             p=next_token(p,1);    // prochain token
  821.             if (p) {
  822.               *p=0;    // octet nul (tableau)
  823.               p++;
  824.             }            
  825.           } while(p!=NULL);
  826.         }
  827.         // recopier arguments actuels (pointeurs uniquement)
  828.         {
  829.           int na;
  830.           for(na=1;na<argc;na++) {
  831.             if (strcmp(argv[na],"-O") != 0)    // SAUF le path!
  832.               x_argv[x_argc++]=argv[na];
  833.             else
  834.               na++;
  835.           }
  836.         }
  837.         argc=x_argc;      // nouvel argc
  838.         argv=x_argv;      // nouvel argv
  839.       }
  840.       
  841.       
  842.     }
  843.     //}
  844.   }
  845.   */
  846.   
  847.   // VΘrifier quiet
  848.   /*
  849.   { 
  850.     int na;    
  851.     for(na=1;na<argc;na++) {
  852.       if (!cmdl_opt(argv[na])) { 
  853.         if ((strcmp(argv[na],"-P")==0) || (strcmp(argv[na],"-N")==0) || (strcmp(argv[na],"-F")==0) || (strcmp(argv[na],"-O")==0) || (strcmp(argv[na],"-V")==0))
  854.           na++;    // sauter nom de proxy
  855.       } else {
  856.         if ((strchr(argv[na],'q')!=NULL) || (strchr(argv[na],'i')!=NULL))
  857.           httrack.quiet=1;    // ne pas poser de questions! (nohup par exemple)
  858.       }
  859.     }
  860.   }
  861.   */
  862.  
  863.   // Pas d'URL
  864. #if DEBUG_STEPS
  865.   printf("Checking URLs\n");
  866. #endif
  867.   if (argv_url==0) {
  868.     // PrΘsence d'un cache, que faire?..
  869.     if (
  870.       ( fexist(fconcat(httrack.path_log,"hts-cache/new.zip")) )
  871.       ||
  872.       ( fexist(fconcat(httrack.path_log,"hts-cache/new.dat")) && fexist(fconcat(httrack.path_log,"hts-cache/new.ndx")) )
  873.       ) {  // il existe dΘja un cache prΘcΘdent.. renommer
  874.       if (fexist(fconcat(httrack.path_log,"hts-cache/doit.log"))) {    // un cache est prΘsent
  875.         if (x_argvblk!=NULL) {
  876.           int m;        
  877.           // Θtablir mode - mode cache: 1 (cache valide) 2 (cache α vΘrifier)
  878.           if (fexist(fconcat(httrack.path_log,"hts-in_progress.lock"))) {    // cache prioritaire
  879.             m=1;
  880.             recuperer=1;
  881.           } else {
  882.             m=2;
  883.           }
  884.           httrack.cache=m;
  885.           
  886.           if (httrack.quiet==0) {  // sinon on continue automatiquement
  887.             HT_REQUEST_START;
  888.             HT_PRINT("A cache (hts-cache/) has been found in the directory ");
  889.             HT_PRINT(httrack.path_log);
  890.             HT_PRINT(LF);
  891.             if (m==1) {
  892.               HT_PRINT("That means that a transfer has been aborted"LF);
  893.               HT_PRINT("OK to Continue ");
  894.             } else {
  895.               HT_PRINT("That means you can update faster the remote site(s)"LF);
  896.               HT_PRINT("OK to Update ");
  897.             }
  898.             HT_PRINT("httrack "); HT_PRINT(x_argvblk); HT_PRINT("?"LF);
  899.             HT_REQUEST_END;
  900.             if (!ask_continue()) { 
  901.               htsmain_free();
  902.               return 0;
  903.             }
  904.           }
  905.           
  906.         } else {
  907.           HTS_PANIC_PRINTF("Error, not enough memory");
  908.           htsmain_free();
  909.           return -1;
  910.         }
  911.       } else { // log existe pas
  912.         HTS_PANIC_PRINTF("A cache has been found, but no command line");
  913.         printf("Please launch httrack with proper parameters to reuse the cache\n");
  914.         htsmain_free();
  915.         return -1;
  916.       }
  917.       
  918.     } else {    // aucune URL dΘfinie et pas de cache
  919.       if (argc > 1 && strcmp(argv[0], "-#h") == 0) {
  920.         printf("HTTrack version "HTTRACK_VERSION"%s\n", WHAT_is_available);
  921.         exit(0);
  922.       }
  923. #if HTS_ANALYSTE!=2
  924.       if (httrack.quiet) {
  925. #endif
  926.         help(argv[0],!httrack.quiet);
  927.         htsmain_free();
  928.         return -1;
  929. #if HTS_ANALYSTE!=2
  930.       } else {
  931.         help_wizard(&httrack);
  932.         htsmain_free();
  933.         return -1;
  934.       }
  935. #endif
  936.       htsmain_free();
  937.       return 0;
  938.     }
  939.   } else {   // plus de 2 paramΦtres
  940.     // un fichier log existe?
  941.     if (fexist(fconcat(httrack.path_log,"hts-in_progress.lock"))) {  // fichier lock?
  942.       //char s[32];
  943.       
  944.       httrack.cache=1;    // cache prioritaire
  945.       if (httrack.quiet==0) {
  946.         if (
  947.           ( fexist(fconcat(httrack.path_log,"hts-cache/new.zip")) )
  948.           ||
  949.           ( fexist(fconcat(httrack.path_log,"hts-cache/new.dat")) && fexist(fconcat(httrack.path_log,"hts-cache/new.ndx")) )
  950.           ) {
  951.           HT_REQUEST_START;
  952.           HT_PRINT("There is a lock-file in the directory ");
  953.           HT_PRINT(httrack.path_log);
  954.           HT_PRINT(LF"That means that a mirror has not been terminated"LF);
  955.           HT_PRINT("Be sure you call httrack with proper parameters"LF);
  956.           HT_PRINT("(The cache allows you to restart faster the transfer)"LF);
  957.           HT_REQUEST_END;
  958.           if (!ask_continue()) {
  959.             htsmain_free();
  960.             return 0;
  961.           }
  962.         }
  963.       }
  964.     } else if (fexist(fconcat(httrack.path_html,"index.html"))) {
  965.       //char s[32];
  966.       httrack.cache=2;  // cache vient aprΦs test de validitΘ
  967.       if (httrack.quiet==0) {
  968.         if (
  969.           ( fexist(fconcat(httrack.path_log,"hts-cache/new.zip")) )
  970.           ||
  971.           ( fexist(fconcat(httrack.path_log,"hts-cache/new.dat")) && fexist(fconcat(httrack.path_log,"hts-cache/new.ndx")) )
  972.           ) {
  973.           HT_REQUEST_START;
  974.           HT_PRINT("There is an index.html and a hts-cache folder in the directory ");
  975.           HT_PRINT(httrack.path_log);
  976.           HT_PRINT(LF"A site may have been mirrored here, that could mean that you want to update it"LF);
  977.           HT_PRINT("Be sure parameters are ok"LF);
  978.           HT_REQUEST_END;
  979.           if (!ask_continue()) {
  980.             htsmain_free();
  981.             return 0;
  982.           }
  983.         } else {
  984.           HT_REQUEST_START;
  985.           HT_PRINT("There is an index.html in the directory ");
  986.           HT_PRINT(httrack.path_log);
  987.           HT_PRINT(" but no cache"LF);
  988.           HT_PRINT("There is an index.html in the directory, but no cache"LF);
  989.           HT_PRINT("A site may have been mirrored here, and erased.."LF);
  990.           HT_PRINT("Be sure parameters are ok"LF);
  991.           HT_REQUEST_END;
  992.           if (!ask_continue()) {
  993.             htsmain_free();
  994.             return 0;
  995.           }
  996.         }
  997.       }
  998.     }
  999.   }
  1000.   
  1001.   
  1002.   // Treat parameters
  1003.   // Traiter les paramΦtres
  1004. #if DEBUG_STEPS
  1005.   printf("Analyze parameters\n");
  1006. #endif
  1007.   { 
  1008.     char* com;
  1009.     int na;
  1010.     
  1011.     for(na=1;na<argc;na++) {
  1012.  
  1013.       if (argv[na][0]=='"') {
  1014.         char BIGSTK tempo[HTS_CDLMAXSIZE];
  1015.         strcpybuff(tempo,argv[na]+1);
  1016.         if (tempo[strlen(tempo)-1]!='"') {
  1017.           char s[HTS_CDLMAXSIZE];
  1018.           sprintf(s,"Missing quote in %s",argv[na]);
  1019.           HTS_PANIC_PRINTF(s);
  1020.           htsmain_free();
  1021.           return -1;
  1022.         }
  1023.         tempo[strlen(tempo)-1]='\0';
  1024.         strcpybuff(argv[na],tempo);
  1025.       }
  1026.  
  1027.       if (cmdl_opt(argv[na])) { // option
  1028.         com=argv[na]+1;
  1029.         
  1030.         while(*com) {
  1031.           switch(*com) {
  1032.           case ' ': case 9: case '-': case '\0': break;
  1033.             //
  1034.           case 'h': 
  1035.             help(argv[0],0); 
  1036.             htsmain_free();
  1037.             return 0;   // dΘja fait normalement
  1038.             //
  1039.           case 'g':    // rΘcupΘrer un (ou plusieurs) fichiers isolΘs
  1040.             httrack.wizard=2;             // le wizard on peut plus s'en passer..
  1041.             //httrack.wizard=0;             // pas de wizard
  1042.             httrack.cache=0;              // ni de cache
  1043.             httrack.makeindex=0;          // ni d'index
  1044.             httrack_logmode=1;            // erreurs α l'Θcran
  1045.             httrack.savename_type=1003;   // mettre dans le rΘpertoire courant
  1046.             httrack.depth=0;              // ne pas explorer la page
  1047.             httrack.accept_cookie=0;      // pas de cookies
  1048.             httrack.robots=0;             // pas de robots
  1049.             break;
  1050.           case 'w': httrack.wizard=2;    // wizard 'soft' (ne pose pas de questions)
  1051.             httrack.travel=0;
  1052.             httrack.seeker=1;
  1053.             break;
  1054.           case 'W': httrack.wizard=1;    // Wizard-Help (pose des questions)
  1055.             httrack.travel=0;
  1056.             httrack.seeker=1;
  1057.             break;
  1058.           case 'r':                      // n'est plus le recurse get bestial mais wizard itou!
  1059.             if (isdigit((unsigned char)*(com+1))) {
  1060.               sscanf(com+1,"%d",&httrack.depth);
  1061.               while(isdigit((unsigned char)*(com+1))) com++;
  1062.             } else httrack.depth=3;
  1063.             break;
  1064. /*
  1065.           case 'r': httrack.wizard=0;
  1066.             if (isdigit((unsigned char)*(com+1))) {
  1067.               sscanf(com+1,"%d",&httrack.depth);
  1068.               while(isdigit((unsigned char)*(com+1))) com++;
  1069.             } else httrack.depth=3;
  1070.             break;
  1071. */
  1072.             //
  1073.             // note: les tests httrack.depth sont pour Θviter de faire
  1074.             // un miroir du web (:-O) accidentelement ;-)
  1075.           case 'a': /*if (httrack.depth==9999) httrack.depth=3;*/
  1076.             httrack.travel=0+(httrack.travel&256); break;
  1077.           case 'd': /*if (httrack.depth==9999) httrack.depth=3;*/
  1078.             httrack.travel=1+(httrack.travel&256); break;
  1079.           case 'l': /*if (httrack.depth==9999) httrack.depth=3;*/
  1080.             httrack.travel=2+(httrack.travel&256); break;
  1081.           case 'e': /*if (httrack.depth==9999) httrack.depth=3;*/
  1082.             httrack.travel=7+(httrack.travel&256); break;
  1083.           case 't': httrack.travel|=256; break;
  1084.           case 'n': httrack.nearlink=1; break;
  1085.           case 'x': httrack.external=1; break;
  1086.             //
  1087.           case 'U': httrack.seeker=2; break;
  1088.           case 'D': httrack.seeker=1; break;
  1089.           case 'S': httrack.seeker=0; break;
  1090.           case 'B': httrack.seeker=3; break;
  1091.             //
  1092.           case 'Y': httrack.mirror_first_page=1; break;
  1093.             //
  1094.           case 'q': case 'i': httrack.quiet=1; break;
  1095.             //
  1096.           case 'Q': httrack_logmode=0; break;
  1097.           case 'v': httrack_logmode=1; break;
  1098.           case 'f': httrack_logmode=2; if (*(com+1)=='2') httrack_logmode=3; while(isdigit((unsigned char)*(com+1))) com++; break;
  1099.             //
  1100.           //case 'A': httrack.urlmode=1; break;
  1101.           //case 'R': httrack.urlmode=2; break;
  1102.           case 'K': httrack.urlmode=0; 
  1103.             if (isdigit((unsigned char)*(com+1))) {
  1104.               sscanf(com+1,"%d",&httrack.urlmode);
  1105.               if (httrack.urlmode == 0) {  // in fact K0 ==> K2
  1106.                                            // and K ==> K0
  1107.                 httrack.urlmode=2;
  1108.               }
  1109.               while(isdigit((unsigned char)*(com+1))) com++; 
  1110.             }
  1111.             //if (*(com+1)=='0') { httrack.urlmode=2; com++; } break;
  1112.             //
  1113.           case 'c':
  1114.             if (isdigit((unsigned char)*(com+1))) {
  1115.               sscanf(com+1,"%d",&httrack.maxsoc);
  1116.               while(isdigit((unsigned char)*(com+1))) com++;
  1117.               httrack.maxsoc=max(httrack.maxsoc,1);     // FORCER A 1
  1118.             } else httrack.maxsoc=4;
  1119.             
  1120.             break;
  1121.             //
  1122.           case 'p': sscanf(com+1,"%d",&httrack.getmode); while(isdigit((unsigned char)*(com+1))) com++; break;
  1123.             //        
  1124.           case 'G': sscanf(com+1,LLintP,&httrack.fragment); while(isdigit((unsigned char)*(com+1))) com++; break;
  1125.           case 'M': sscanf(com+1,LLintP,&httrack.maxsite); while(isdigit((unsigned char)*(com+1))) com++; break;
  1126.           case 'm': sscanf(com+1,LLintP,&httrack.maxfile_nonhtml); while(isdigit((unsigned char)*(com+1))) com++; 
  1127.             if (*(com+1)==',') {
  1128.               com++;
  1129.               sscanf(com+1,LLintP,&httrack.maxfile_html); while(isdigit((unsigned char)*(com+1))) com++;
  1130.             } else httrack.maxfile_html=-1;
  1131.             break;
  1132.             //
  1133.           case 'T': sscanf(com+1,"%d",&httrack.timeout); while(isdigit((unsigned char)*(com+1))) com++; break;
  1134.           case 'J': sscanf(com+1,"%d",&httrack.rateout); while(isdigit((unsigned char)*(com+1))) com++; break;
  1135.           case 'R': sscanf(com+1,"%d",&httrack.retry); while(isdigit((unsigned char)*(com+1))) com++; break;
  1136.           case 'E': sscanf(com+1,"%d",&httrack.maxtime); while(isdigit((unsigned char)*(com+1))) com++; break;
  1137.           case 'H': sscanf(com+1,"%d",&httrack.hostcontrol); while(isdigit((unsigned char)*(com+1))) com++; break;
  1138.           case 'A': sscanf(com+1,"%d",&httrack.maxrate); while(isdigit((unsigned char)*(com+1))) com++; break;
  1139.  
  1140.           case 'j': httrack.parsejava=1; if (*(com+1)=='0') { httrack.parsejava=0; com++; } break;
  1141.             //
  1142.           case 'I': httrack.makeindex=1; if (*(com+1)=='0') { httrack.makeindex=0; com++; } break;
  1143.             //
  1144.           case 'X': httrack.delete_old=1; if (*(com+1)=='0') { httrack.delete_old=0; com++; } break;
  1145.             //
  1146.           case 'b': sscanf(com+1,"%d",&httrack.accept_cookie); while(isdigit((unsigned char)*(com+1))) com++; break;
  1147.             //
  1148.           case 'N':
  1149.             if (strcmp(argv[na],"-N")==0) {    // Tout seul
  1150.               if ((na+1>=argc) || (argv[na+1][0]=='-')) {  // erreur
  1151.                 HTS_PANIC_PRINTF("Option N needs a number, or needs to be followed by a blank space, and a string");
  1152.                 printf("Example: -N4\n");
  1153.                 htsmain_free();
  1154.                 return -1;
  1155.               } else {
  1156.                 na++;
  1157.                 if (strlen(argv[na])>=127) {
  1158.                   HTS_PANIC_PRINTF("Userdef structure string too long");
  1159.                   htsmain_free();
  1160.                   return -1;
  1161.                 }
  1162.                 strcpybuff(httrack.savename_userdef,argv[na]);
  1163.                 if (strnotempty(httrack.savename_userdef))
  1164.                   httrack.savename_type = -1;    // userdef!
  1165.                 else
  1166.                   httrack.savename_type = 0;    // -N "" : par dΘfaut
  1167.               }
  1168.             } else {
  1169.               sscanf(com+1,"%d",&httrack.savename_type); while(isdigit((unsigned char)*(com+1))) com++;
  1170.             }
  1171.             break;
  1172.           case 'L': 
  1173.             {
  1174.               sscanf(com+1,"%d",&httrack.savename_83); 
  1175.               switch(httrack.savename_83) {
  1176.               case 0:    // 8-3 (ISO9660 L1)
  1177.                 httrack.savename_83=1;
  1178.                 break;
  1179.               case 1:
  1180.                 httrack.savename_83=0;
  1181.                 break;
  1182.               default:    // 2 == ISO9660 (ISO9660 L2)
  1183.                 httrack.savename_83=2;
  1184.                 break;
  1185.               }
  1186.               while(isdigit((unsigned char)*(com+1))) com++; 
  1187.             }
  1188.             break;
  1189.           case 's': 
  1190.             if (isdigit((unsigned char)*(com+1))) {
  1191.               sscanf(com+1,"%d",&httrack.robots);
  1192.               while(isdigit((unsigned char)*(com+1))) com++;
  1193.             } else httrack.robots=1;
  1194. #if DEBUG_ROBOTS
  1195.             printf("robots.txt mode set to %d\n",httrack.robots);
  1196. #endif
  1197.             break;
  1198.           case 'o': sscanf(com+1,"%d",&httrack.errpage); while(isdigit((unsigned char)*(com+1))) com++; break;
  1199.           case 'u': sscanf(com+1,"%d",&httrack.check_type); while(isdigit((unsigned char)*(com+1))) com++; break;
  1200.             //
  1201.           case 'C': 
  1202.             if (isdigit((unsigned char)*(com+1))) {
  1203.               sscanf(com+1,"%d",&httrack.cache);
  1204.               while(isdigit((unsigned char)*(com+1))) com++;
  1205.             } else httrack.cache=1;
  1206.             break;
  1207.           case 'k': httrack.all_in_cache=1; break;
  1208.             //
  1209.           case 'z': httrack.debug=1; break;  // petit debug
  1210.           case 'Z': httrack.debug=2; break;  // GROS debug
  1211.             //
  1212.           case '&': case '%': {    // deuxiΦme jeu d'options
  1213.             com++;
  1214.             switch(*com) {
  1215.             case 'M': httrack.mimehtml = 1; if (*(com+1)=='0') { httrack.mimehtml=0; com++; } break;
  1216.             case 'k': httrack.nokeepalive = 0; if (*(com+1)=='0') { httrack.nokeepalive = 1; com++; } break;
  1217.             case 'x': httrack.passprivacy=1; if (*(com+1)=='0') { httrack.passprivacy=0; com++; } break;   // No passwords in html files
  1218.             case 'q': httrack.includequery=1; if (*(com+1)=='0') { httrack.includequery=0; com++; } break;   // No passwords in html files
  1219.             case 'I': httrack.kindex=1; if (isdigit((unsigned char)*(com+1))) { sscanf(com+1,"%d",&httrack.kindex); while(isdigit((unsigned char)*(com+1))) com++; }
  1220.               break;    // Keyword Index
  1221.             case 'c': sscanf(com+1,"%f",&httrack.maxconn); while(isdigit((unsigned char)*(com+1)) || *(com+1) == '.') com++; break;
  1222.             case 'e': sscanf(com+1,"%d",&httrack.extdepth); while(isdigit((unsigned char)*(com+1))) com++; break;
  1223.             case 'B': httrack.tolerant=1; if (*(com+1)=='0') { httrack.tolerant=0; com++; } break;   // HTTP/1.0 notamment
  1224.             case 'h': httrack.http10=1; if (*(com+1)=='0') { httrack.http10=0; com++; } break;   // HTTP/1.0
  1225.             case 'z': httrack.nocompression=1; if (*(com+1)=='0') { httrack.nocompression=0; com++; } break;   // pas de compression
  1226.             case 'f': httrack.ftp_proxy=1; if (*(com+1)=='0') { httrack.ftp_proxy=0; com++; } break;   // proxy http pour ftp
  1227.             case 'P': httrack.parseall=1; if (*(com+1)=='0') { httrack.parseall=0; com++; } break;   // tout parser
  1228.             case 'n': httrack.norecatch=1; if (*(com+1)=='0') { httrack.norecatch=0; com++; } break;   // ne pas reprendre fichiers effacΘs localement
  1229.             case 's': httrack.sizehack=1; if (*(com+1)=='0') { httrack.sizehack=0; com++; } break;   // hack sur content-length
  1230.             case 'u': httrack.urlhack=1; if (*(com+1)=='0') { httrack.urlhack=0; com++; } break;   // url hack
  1231.             case 'v': httrack.verbosedisplay=2; if (isdigit((unsigned char)*(com+1))) { sscanf(com+1,"%d",&httrack.verbosedisplay); while(isdigit((unsigned char)*(com+1))) com++; } break;
  1232.             case 'i': httrack.dir_topindex = 1; if (*(com+1)=='0') { httrack.dir_topindex=0; com++; } break;
  1233.  
  1234.             // preserve: no footer, original links
  1235.             case 'p':
  1236.               httrack.footer[0]='\0';
  1237.               httrack.urlmode=4;
  1238.               break;
  1239.             case 'L':    // URL list
  1240.               if ((na+1>=argc) || (argv[na+1][0]=='-')) {
  1241.                 HTS_PANIC_PRINTF("Option %L needs to be followed by a blank space, and a text filename");
  1242.                 printf("Example: -%%L \"mylist.txt\"\n");
  1243.                 htsmain_free();
  1244.                 return -1;
  1245.               } else{
  1246.                 na++;
  1247.                 if (strlen(argv[na])>=254) {
  1248.                   HTS_PANIC_PRINTF("File list string too long");
  1249.                   htsmain_free();
  1250.                   return -1;
  1251.                 }
  1252.                 strcpybuff(httrack.filelist,argv[na]);
  1253.               }
  1254.               break;
  1255.             case 'b':  // bind
  1256.               if ((na+1>=argc) || (argv[na+1][0]=='-')) {
  1257.                 HTS_PANIC_PRINTF("Option %b needs to be followed by a blank space, and a local hostname");
  1258.                 printf("Example: -%%b \"ip4.localhost\"\n");
  1259.                 htsmain_free();
  1260.                 return -1;
  1261.               } else{
  1262.                 na++;
  1263.                 if (strlen(argv[na])>=254) {
  1264.                   HTS_PANIC_PRINTF("Hostname string too long");
  1265.                   htsmain_free();
  1266.                   return -1;
  1267.                 }
  1268.                 strcpybuff(httrack.proxy.bindhost,argv[na]);
  1269.               }
  1270.               break;
  1271.             case 'S':    // Scan Rules list
  1272.               if ((na+1>=argc) || (argv[na+1][0]=='-')) {
  1273.                 HTS_PANIC_PRINTF("Option %S needs to be followed by a blank space, and a text filename");
  1274.                 printf("Example: -%%S \"myfilterlist.txt\"\n");
  1275.                 htsmain_free();
  1276.                 return -1;
  1277.               } else{
  1278.                 INTsys fz;
  1279.                 na++;
  1280.                 fz = fsize(argv[na]);
  1281.                 if (fz < 0) {
  1282.                   HTS_PANIC_PRINTF("File url list could not be opened");
  1283.                   htsmain_free();
  1284.                   return -1;
  1285.                 } else {
  1286.                   FILE* fp = fopen(argv[na], "rb");
  1287.                   if (fp != NULL) {
  1288.                     int cl = (int) strlen(url);
  1289.                     ensureUrlCapacity(url, url_sz, cl + fz + 8192);
  1290.                     if ((INTsys)fread(url + cl, 1, fz, fp) != fz) {
  1291.                       HTS_PANIC_PRINTF("File url list could not be read");
  1292.                       htsmain_free();
  1293.                       return -1;
  1294.                     }
  1295.                     fclose(fp);
  1296.                     *(url + cl + fz) = '\0';
  1297.                   }
  1298.                 }
  1299.               }
  1300.               break;
  1301.             case 'A':    // assume
  1302.               if ((na+1>=argc) || (argv[na+1][0]=='-')) {
  1303.                 HTS_PANIC_PRINTF("Option %A needs to be followed by a blank space, and a filesystemtype=mimetype/mimesubtype parameters");
  1304.                 printf("Example: -%%A php3=text/html,asp=text/html\n");
  1305.                 htsmain_free();
  1306.                 return -1;
  1307.               } else{
  1308.                 char* a;
  1309.                 na++;
  1310.                 if ( (strlen(argv[na]) + strlen(httrack.mimedefs) + 4) >= sizeof(httrack.mimedefs)) {
  1311.                   HTS_PANIC_PRINTF("Mime definition string too long");
  1312.                   htsmain_free();
  1313.                   return -1;
  1314.                 }
  1315.                 // --assume standard
  1316.                 if (strcmp(argv[na],"standard") == 0) {
  1317.                   strcpybuff(httrack.mimedefs,"\n");
  1318.                   strcatbuff(httrack.mimedefs,HTS_ASSUME_STANDARD);
  1319.                   strcatbuff(httrack.mimedefs,"\n");
  1320.                 } else {
  1321.                   strcatbuff(httrack.mimedefs,argv[na]);
  1322.                   strcatbuff(httrack.mimedefs,"\n");
  1323.                 }
  1324.                 a=httrack.mimedefs;
  1325.                 while(*a) {
  1326.                   switch(*a) {
  1327.                   case ',': case ' ': case '\r': case ';': case '\t':
  1328.                     *a='\n';
  1329.                     break;
  1330.                   }
  1331.                   a++;
  1332.                 }
  1333.               }
  1334.               break;
  1335.               //
  1336.             case 'l': 
  1337.               if ((na+1>=argc) || (argv[na+1][0]=='-')) {
  1338.                 HTS_PANIC_PRINTF("Option %l needs to be followed by a blank space, and an ISO language code");
  1339.                 printf("Example: -%%l \"en\"\n");
  1340.                 htsmain_free();
  1341.                 return -1;
  1342.               } else{
  1343.                 na++;
  1344.                 if (strlen(argv[na])>=62) {
  1345.                   HTS_PANIC_PRINTF("Lang list string too long");
  1346.                   htsmain_free();
  1347.                   return -1;
  1348.                 }
  1349.                 strcpybuff(httrack.lang_iso,argv[na]);
  1350.               }
  1351.               break;
  1352.               //
  1353.             case 'F':     // footer id
  1354.               if ((na+1>=argc) || (argv[na+1][0]=='-')) {
  1355.                 HTS_PANIC_PRINTF("Option %F needs to be followed by a blank space, and a footer string");
  1356.                 printf("Example: -%%F \"<!-- Mirrored from %%s by HTTrack Website Copier/"HTTRACK_AFF_VERSION" "HTTRACK_AFF_AUTHORS", %%s -->\"\n");
  1357.                 htsmain_free();
  1358.                 return -1;
  1359.               } else{
  1360.                 na++;
  1361.                 if (strlen(argv[na])>=254) {
  1362.                   HTS_PANIC_PRINTF("Footer string too long");
  1363.                   htsmain_free();
  1364.                   return -1;
  1365.                 }
  1366.                 strcpybuff(httrack.footer,argv[na]);
  1367.               }
  1368.               break;
  1369.             case 'H':                 // debug headers
  1370.               _DEBUG_HEAD=1;
  1371.               break;
  1372.             case 'O':
  1373. #if HTS_WIN
  1374.               printf("Warning option -%%O has no effect in this system (chroot)\n");
  1375. #else
  1376.               switch_chroot=1;
  1377. #endif
  1378.               break;
  1379.             case 'U':                 // setuid
  1380.               if ((na+1>=argc) || (argv[na+1][0]=='-')) {
  1381.                 HTS_PANIC_PRINTF("Option %U needs to be followed by a blank space, and a username");
  1382.                 printf("Example: -%%U smith\n");
  1383.                 htsmain_free();
  1384.                 return -1;
  1385.               } else {
  1386.                 na++;
  1387. #if HTS_WIN
  1388.                 printf("Warning option -%%U has no effect on this system (setuid)\n");
  1389. #else
  1390. #ifndef HTS_DO_NOT_USE_UID
  1391.                 /* Change the user id and gid */
  1392.                 {
  1393.                   struct passwd* userdef=getpwnam((const char*)argv[na]);
  1394.                   if (userdef) {    /* we'll have to switch the user id */
  1395.                     switch_gid=userdef->pw_gid;
  1396.                     switch_uid=userdef->pw_uid;
  1397.                   }
  1398.                 }
  1399. #else
  1400.                 printf("Warning option -%%U has no effect with this compiled version (setuid)\n");
  1401. #endif
  1402. #endif
  1403.               }
  1404.               break;
  1405.               
  1406.             case 'W':       // Wrapper callback
  1407.               // --wrapper check-link=obj.so:check_link
  1408.               if ((na+1>=argc) || (argv[na+1][0]=='-')) {
  1409.                 HTS_PANIC_PRINTF("Option %W needs to be followed by a blank space, and a <callback-name>=<myfile.so>:<function-name> field");
  1410.                 printf("Example: -%%W check-link=checklink.so:check\n");
  1411.                 htsmain_free();
  1412.                 return -1;
  1413.               } else {
  1414.                 char callbackname[128];
  1415.                 char* a = argv[na + 1];
  1416.                 char* pos = strchr(a, '=');
  1417.                 na++;
  1418.                 if (pos != NULL && (pos - a) > 0 && (pos - a + 2) < sizeof(callbackname)) {
  1419.                   char* posf = strchr(pos + 1, ':');
  1420.                   char BIGSTK filename[1024];
  1421.                   callbackname[0] = '\0';
  1422.                   strncatbuff(callbackname, a, pos - a);
  1423.                   pos++;
  1424.                   if (posf != NULL && (posf - pos) > 0 && (posf - pos + 2) < sizeof(filename)) {
  1425.                     void* userfunction;
  1426.                     filename[0] = '\0';
  1427.                     strncatbuff(filename, pos, posf - pos);
  1428.                     posf++;
  1429.                     userfunction = getFunctionPtr(&httrack, filename, posf);
  1430.                     if (userfunction != NULL) {
  1431.                       if ((void*)htswrap_read(callbackname) != NULL) {
  1432.                         if (htswrap_add(callbackname, userfunction)) {
  1433.                           set_wrappers();        /* Re-read wrappers internal static functions */
  1434.                           if ((void*)htswrap_read(callbackname) == userfunction) {
  1435.                             if (!httrack.quiet) {
  1436.                               fprintf(stderr, "successfully plugged [%s -> %s:%s]\n", callbackname, posf, filename);
  1437.                             }
  1438.                           } else {
  1439.                             char BIGSTK tmp[1024 * 2];
  1440.                             sprintf(tmp, "option %%W : unable to (re)plug the function %s from the file %s for the callback %s", posf, filename, callbackname);
  1441.                             HTS_PANIC_PRINTF(tmp);
  1442.                             htsmain_free();
  1443.                             return -1;
  1444.                           }
  1445.                         } else {
  1446.                           char BIGSTK tmp[1024 * 2];
  1447.                           sprintf(tmp, "option %%W : unable to plug the function %s from the file %s for the callback %s", posf, filename, callbackname);
  1448.                           HTS_PANIC_PRINTF(tmp);
  1449.                           htsmain_free();
  1450.                           return -1;
  1451.                         }
  1452.                       } else {
  1453.                         char BIGSTK tmp[1024 * 2];
  1454.                         sprintf(tmp, "option %%W : unknown or undefined callback %s", callbackname);
  1455.                         HTS_PANIC_PRINTF(tmp);
  1456.                         htsmain_free();
  1457.                         return -1;
  1458.                       }
  1459.                     } else {
  1460.                       char BIGSTK tmp[1024 * 2];
  1461.                       sprintf(tmp, "option %%W : unable to load the function %s in the file %s for the callback %s", posf, filename, callbackname);
  1462.                       HTS_PANIC_PRINTF(tmp);
  1463.                       htsmain_free();
  1464.                       return -1;
  1465.                     }
  1466.                   } else {
  1467.                     HTS_PANIC_PRINTF("Syntax error in option %W : filename error : this function needs to be followed by a blank space, and a <callback-name>=<myfile.so>:<function-name> field");
  1468.                     printf("Example: -%%W check-link=checklink.so:check\n");
  1469.                     htsmain_free();
  1470.                     return -1;
  1471.                   }
  1472.                 } else {
  1473.                   HTS_PANIC_PRINTF("Syntax error in option %W : this function needs to be followed by a blank space, and a <callback-name>=<myfile.so>:<function-name> field");
  1474.                   printf("Example: -%%W check-link=checklink.so:check\n");
  1475.                   htsmain_free();
  1476.                   return -1;
  1477.                 }
  1478.               }
  1479.               break;
  1480.               
  1481.             case 'R':    // Referer
  1482.               if ((na+1>=argc) || (argv[na+1][0]=='-')) {
  1483.                 HTS_PANIC_PRINTF("Option %R needs to be followed by a blank space, and a referer URL");
  1484.                 printf("Example: -%%R \"http://www.example.com/\"\n");
  1485.                 htsmain_free();
  1486.                 return -1;
  1487.               } else{
  1488.                 na++;
  1489.                 if (strlen(argv[na])>=254) {
  1490.                   HTS_PANIC_PRINTF("Referer URL too long");
  1491.                   htsmain_free();
  1492.                   return -1;
  1493.                 }
  1494.                 strcpybuff(httrack.referer, argv[na]);
  1495.               }
  1496.               break;
  1497.             case 'E':    // From Email address
  1498.               if ((na+1>=argc) || (argv[na+1][0]=='-')) {
  1499.                 HTS_PANIC_PRINTF("Option %E needs to be followed by a blank space, and an email");
  1500.                 printf("Example: -%%E \"postmaster@example.com\"\n");
  1501.                 htsmain_free();
  1502.                 return -1;
  1503.               } else{
  1504.                 na++;
  1505.                 if (strlen(argv[na])>=254) {
  1506.                   HTS_PANIC_PRINTF("From email too long");
  1507.                   htsmain_free();
  1508.                   return -1;
  1509.                 }
  1510.                 strcpybuff(httrack.from, argv[na]);
  1511.               }
  1512.               break;
  1513.  
  1514.             default: {
  1515.               char s[HTS_CDLMAXSIZE];
  1516.               sprintf(s,"invalid option %%%c\n",*com);
  1517.               HTS_PANIC_PRINTF(s);
  1518.               htsmain_free();
  1519.               return -1;
  1520.                      }
  1521.               break;
  1522.               
  1523.             }
  1524.                     }
  1525.             break;
  1526.             //
  1527.           case '@': {    // troisiΦme jeu d'options
  1528.             com++;
  1529.             switch(*com) {
  1530.             case 'i': 
  1531. #if HTS_INET6==0
  1532.               printf("Warning, option @i has no effect (v6 routines not compiled)\n");
  1533. #else 
  1534.               {
  1535.                 int res=0;
  1536.                 if (isdigit((unsigned char)*(com+1))) {
  1537.                   sscanf(com+1,"%d",&res); while(isdigit((unsigned char)*(com+1))) com++; 
  1538.                 }
  1539.                 switch(res) {
  1540.                 case 1:
  1541.                 case 4:
  1542.                   IPV6_resolver=1;
  1543.                   break;
  1544.                 case 2:
  1545.                 case 6:
  1546.                   IPV6_resolver=2;
  1547.                   break;
  1548.                 case 0:
  1549.                   IPV6_resolver=0;
  1550.                   break;
  1551.                 default:
  1552.                   printf("Unknown flag @i%d\n", res);
  1553.                   htsmain_free();
  1554.                   return -1;
  1555.                   break;
  1556.                 }
  1557.               }
  1558. #endif
  1559.               break;
  1560.               
  1561.                 default: {
  1562.                   char s[HTS_CDLMAXSIZE];
  1563.                   sprintf(s,"invalid option %%%c\n",*com);
  1564.                   HTS_PANIC_PRINTF(s);
  1565.                   htsmain_free();
  1566.                   return -1;
  1567.                          }
  1568.                   break;
  1569.                   
  1570.                   //case 's': httrack.sslengine=1; if (isdigit((unsigned char)*(com+1))) { sscanf(com+1,"%d",&httrack.sslengine); while(isdigit((unsigned char)*(com+1))) com++; } break;
  1571.             }
  1572.                     }
  1573.             break;
  1574.             
  1575.             //
  1576.           case '#':  { // non documentΘ
  1577.             com++;
  1578.             switch(*com) {
  1579.             case 'C':   // list cache files : httrack -#C '*spid*.gif' will attempt to find the matching file
  1580.               {
  1581.                 int hasFilter = 0;
  1582.                 int found = 0;
  1583.                 char* filter=NULL;
  1584.                 cache_back cache;
  1585.                 inthash cache_hashtable=inthash_new(HTS_HASH_SIZE);
  1586.                 int backupXFR = htsMemoryFastXfr;
  1587.                 int sendb = 0;
  1588.                 if (isdigit((unsigned char)*(com+1))) {
  1589.                   sscanf(com+1,"%d",&sendb);
  1590.                   while(isdigit((unsigned char)*(com+1))) com++;
  1591.                 } else sendb=0;
  1592.                 if (!((na+1>=argc) || (argv[na+1][0]=='-'))) {
  1593.                   na++;
  1594.                   hasFilter = 1;
  1595.                   filter=argv[na];
  1596.                 }
  1597.                 htsMemoryFastXfr = 1;               /* fast load */
  1598.  
  1599.                 memset(&cache, 0, sizeof(cache_back));
  1600.                 cache.type=1;       // cache?
  1601.                 cache.log=stdout;     // log?
  1602.                 cache.errlog=stderr;  // err log?
  1603.                 cache.ptr_ant=cache.ptr_last=0;   // pointeur pour anticiper
  1604.                 cache.hashtable=(void*)cache_hashtable;      /* copy backcache hash */
  1605.                 cache.ro = 1;          /* read only */
  1606.                 if (cache.hashtable) {
  1607.                   char BIGSTK adr[HTS_URLMAXSIZE*2];
  1608.                   char BIGSTK fil[HTS_URLMAXSIZE*2];
  1609.                   char BIGSTK url[HTS_URLMAXSIZE*2];
  1610.                   char linepos[256];
  1611.                   int  pos;
  1612.                   char* cacheNdx = readfile(fconcat(httrack.path_log,"hts-cache/new.ndx"));
  1613.                   cache_init(&cache,&httrack);            /* load cache */
  1614.                   if (cacheNdx != NULL) {
  1615.                     char firstline[256];
  1616.                     char* a = cacheNdx;
  1617.                     a+=cache_brstr(a, firstline);
  1618.                     a+=cache_brstr(a, firstline);
  1619.                     while ( a != NULL ) {
  1620.                       a=strchr(a+1,'\n');     /* start of line */
  1621.                       if (a) {
  1622.                         htsblk r;
  1623.                         /* */
  1624.                         a++;
  1625.                         /* read "host/file" */
  1626.                         a+=binput(a,adr,HTS_URLMAXSIZE);
  1627.                         a+=binput(a,fil,HTS_URLMAXSIZE);
  1628.                         url[0]='\0';
  1629.                         if (!link_has_authority(adr))
  1630.                           strcatbuff(url, "http://");
  1631.                         strcatbuff(url, adr);
  1632.                         strcatbuff(url, fil);
  1633.                         /* read position */
  1634.                         a+=binput(a,linepos,200);
  1635.                         sscanf(linepos,"%d",&pos);
  1636.                         if (!hasFilter
  1637.                           ||
  1638.                           (strjoker(url, filter, NULL, NULL) != NULL)
  1639.                           ) {
  1640.                           r = cache_read_ro(&httrack, &cache, adr, fil, "", NULL);    // lire entrΘe cache + data
  1641.                           if (r.statuscode != -1) {    // No errors
  1642.                             found++;
  1643.                             if (!hasFilter) {
  1644.                               fprintf(stdout, "%s%s%s\r\n", 
  1645.                                 (link_has_authority(adr)) ? "" : "http://", 
  1646.                                 adr, fil);
  1647.                             } else {
  1648.                               char msg[256], cdate[256];
  1649.                               char BIGSTK sav[HTS_URLMAXSIZE*2];
  1650.                               infostatuscode(msg, r.statuscode);
  1651.                               time_gmt_rfc822(cdate);
  1652.  
  1653.                               fprintf(stdout, "HTTP/1.1 %d %s\r\n",
  1654.                                 r.statuscode,
  1655.                                 r.msg[0] ? r.msg : msg
  1656.                                 );
  1657.                               fprintf(stdout, "X-Host: %s\r\n", adr);
  1658.                               fprintf(stdout, "X-File: %s\r\n", fil);
  1659.                               fprintf(stdout, "X-URL: %s%s%s\r\n", 
  1660.                                 (link_has_authority(adr)) ? "" : "http://", 
  1661.                                 adr, fil);
  1662.                               if (url_savename(adr, fil, sav, NULL, NULL, NULL, NULL,
  1663.                                 &httrack, NULL, 0, NULL, 0, &cache, NULL, 0, 0)!=-1) {
  1664.                                 if (fexist(sav)) {
  1665.                                   fprintf(stdout, "Content-location: %s\r\n", sav);
  1666.                                 }
  1667.                               }
  1668.                               fprintf(stdout, "Date: %s\r\n", cdate);
  1669.                               fprintf(stdout, "Server: HTTrack Website Copier/"HTTRACK_VERSION"\r\n");
  1670.                               if (r.lastmodified[0]) {
  1671.                                 fprintf(stdout, "Last-Modified: %s\r\n", r.lastmodified);
  1672.                               }
  1673.                               if (r.etag[0]) {
  1674.                                 fprintf(stdout, "Etag: %s\r\n", r.etag);
  1675.                               }
  1676.                               if (r.totalsize >= 0) {
  1677.                                 fprintf(stdout, "Content-Length: "LLintP"\r\n", r.totalsize);
  1678.                               }
  1679.                               fprintf(stdout, "X-Content-Length: "LLintP"\r\n", (r.size >= 0) ? r.size : (-r.size) );
  1680.                               if (r.contenttype >= 0) {
  1681.                                 fprintf(stdout, "Content-Type: %s\r\n", r.contenttype);
  1682.                               }
  1683.                               if (r.cdispo[0]) {
  1684.                                 fprintf(stdout, "Content-Disposition: %s\r\n", r.cdispo);
  1685.                               }
  1686.                               if (r.contentencoding[0]) {
  1687.                                 fprintf(stdout, "Content-Encoding: %s\r\n", r.contentencoding);
  1688.                               }
  1689.                               if (r.is_chunk) {
  1690.                                 fprintf(stdout, "Transfer-Encoding: chunked\r\n");
  1691.                               }
  1692. #if HTS_USEOPENSSL
  1693.                               if (r.ssl) {
  1694.                                 fprintf(stdout, "X-SSL: yes\r\n");
  1695.                               }
  1696. #endif
  1697.                               if (r.is_write) {
  1698.                                 fprintf(stdout, "X-Direct-To-Disk: yes\r\n");
  1699.                               }
  1700.                               if (r.compressed) {
  1701.                                 fprintf(stdout, "X-Compressed: yes\r\n");
  1702.                               }
  1703.                               if (r.notmodified) {
  1704.                                 fprintf(stdout, "X-Not-Modified: yes\r\n");
  1705.                               }
  1706.                               if (r.is_chunk) {
  1707.                                 fprintf(stdout, "X-Chunked: yes\r\n");
  1708.                               }
  1709.                               fprintf(stdout, "\r\n");
  1710.                               /* Send the body */
  1711.                               if (sendb && r.adr) {
  1712.                                 fprintf(stdout, "%s\r\n", r.adr);
  1713.                               }
  1714.                             }
  1715.                           }
  1716.                         }
  1717.                       }
  1718.                     }
  1719.                     freet(cacheNdx);
  1720.                   }
  1721.                 }
  1722.                 if (!found) {
  1723.                   fprintf(stderr, "No cache entry found%s%s%s\r\n",
  1724.                     (hasFilter)?" for '":"",
  1725.                     (hasFilter)?filter:"",
  1726.                     (hasFilter)?"'":""
  1727.                     );
  1728.                 }
  1729.                 htsMemoryFastXfr = backupXFR;
  1730.                 return 0;
  1731.               }
  1732.               break;
  1733.             case 'E':     // extract cache
  1734.               if (!hts_extract_meta(httrack.path_log)) {
  1735.                 fprintf(stderr, "* error extracting meta-data\n");
  1736.                 return 1;
  1737.               }
  1738.               fprintf(stderr, "* successfully extracted meta-data\n");
  1739.               return 0;
  1740.               break;
  1741.             case 'X': 
  1742. #ifndef STRDEBUG
  1743.               fprintf(stderr, "warning: no string debugging support built, option has no effect\n");
  1744. #endif
  1745.               htsMemoryFastXfr=1; 
  1746.               if (*(com+1)=='0') { htsMemoryFastXfr=0; com++; } 
  1747.               break;
  1748.             case 'R':
  1749.               {
  1750.                 char* name;
  1751.                 uLong repaired = 0;
  1752.                 uLong repairedBytes = 0;
  1753.                 if (fexist(fconcat(httrack.path_log,"hts-cache/new.zip"))) {
  1754.                   name = fconcat(httrack.path_log,"hts-cache/new.zip");
  1755.                 } else if (fexist(fconcat(httrack.path_log,"hts-cache/old.zip"))) {
  1756.                   name = fconcat(httrack.path_log,"hts-cache/old.zip");
  1757.                 } else {
  1758.                   fprintf(stderr, "* error: no cache found in %s\n", fconcat(httrack.path_log,"hts-cache/new.zip"));
  1759.                   return 1;
  1760.                 }
  1761.                 fprintf(stderr, "Cache: trying to repair %s\n", name);
  1762.                 if (unzRepair(name, 
  1763.                   fconcat(httrack.path_log,"hts-cache/repair.zip"),
  1764.                   fconcat(httrack.path_log,"hts-cache/repair.tmp"),
  1765.                   &repaired, &repairedBytes
  1766.                   ) == Z_OK) {
  1767.                   unlink(name);
  1768.                   rename(fconcat(httrack.path_log,"hts-cache/repair.zip"), name);
  1769.                   fprintf(stderr,"Cache: %d bytes successfully recovered in %d entries\n", (int) repairedBytes, (int) repaired);
  1770.                 } else {
  1771.                   fprintf(stderr, "Cache: could not repair the cache\n");
  1772.                 }
  1773.               }
  1774.               return 0;
  1775.               break;
  1776.             case '~': /* internal lib test */
  1777.               {
  1778.                 char thisIsATestYouShouldSeeAnError[12];
  1779.                 strcpybuff(thisIsATestYouShouldSeeAnError, "0123456789012345678901234567890123456789");
  1780.                 return 0;
  1781.               }
  1782.               break;
  1783.             case 'f': httrack.flush=1; break;
  1784.             case 'h':
  1785.               printf("HTTrack version "HTTRACK_VERSION"%s\n", WHAT_is_available);
  1786.               return 0;
  1787.               break;
  1788.             case 'p': /* httrack.aff_progress=1; deprecated */ break;
  1789.             case 'S': httrack.shell=1; break;  // stdin sur un shell
  1790.             case 'K': httrack.keyboard=1; break;  // vΘrifier stdin
  1791.               //
  1792.             case 'L': sscanf(com+1,"%d",&httrack.maxlink); while(isdigit((unsigned char)*(com+1))) com++; break;
  1793.             case 'F': sscanf(com+1,"%d",&httrack.maxfilter); while(isdigit((unsigned char)*(com+1))) com++; break;
  1794.             case 'Z': httrack.makestat=1; break;
  1795.             case 'T': httrack.maketrack=1; break;
  1796.             case 'u': sscanf(com+1,"%d",&httrack.waittime); while(isdigit((unsigned char)*(com+1))) com++; break;
  1797.  
  1798.             /*case 'R':    // ohh ftp, catch->ftpget
  1799.               HTS_PANIC_PRINTF("Unexpected internal error with -#R command");
  1800.               htsmain_free();
  1801.               return -1;        
  1802.               break;
  1803.               */
  1804.             case 'P': {     // catchurl
  1805.               help_catchurl(httrack.path_log);
  1806.               htsmain_free();
  1807.               return 0;
  1808.                       }
  1809.               break;
  1810.           
  1811.             case '0':   /* test #0 : filters */
  1812.               if (na+2>=argc) {
  1813.                 HTS_PANIC_PRINTF("Option #0 needs to be followed by a filter string and a string");
  1814.                 printf("Example: '-#0' '*.gif' 'foo.gif'\n");
  1815.                 htsmain_free();
  1816.                 return -1;
  1817.               } else {
  1818.                 if (strjoker(argv[na+2],argv[na+1],NULL,NULL))
  1819.                   printf("%s does match %s\n",argv[na+2],argv[na+1]);
  1820.                 else
  1821.                   printf("%s does NOT match %s\n",argv[na+2],argv[na+1]);
  1822.                 htsmain_free();
  1823.                 return 0;
  1824.               }
  1825.               break;
  1826.             case '1':   /* test #1 : fil_simplifie */
  1827.               if (na+1>=argc) {
  1828.                 HTS_PANIC_PRINTF("Option #1 needs to be followed by an URL");
  1829.                 printf("Example: '-#0' ./foo/bar/../foobar\n");
  1830.                 htsmain_free();
  1831.                 return -1;
  1832.               } else {
  1833.                 fil_simplifie(argv[na+1]);
  1834.                 printf("simplified=%s\n", argv[na+1]);
  1835.                 htsmain_free();
  1836.                 return 0;
  1837.               }
  1838.               break;
  1839.             case '!':
  1840.               if (na+1>=argc) {
  1841.                 HTS_PANIC_PRINTF("Option #! needs to be followed by a commandline");
  1842.                 printf("Example: '-#!' 'echo hello'\n");
  1843.                 htsmain_free();
  1844.                 return -1;
  1845.               } else {
  1846.                 system(argv[na+1]);
  1847.               }
  1848.               break;
  1849.             case 'd':
  1850.               httrack.parsedebug = 1;
  1851.               break;
  1852.  
  1853.             /* autotest */
  1854.             case 't':     /* not yet implemented */
  1855.               fprintf(stderr, "** AUTOCHECK OK\n");
  1856.               exit(0);
  1857.               break;
  1858.  
  1859.             default: printf("Internal option %c not recognized\n",*com); break;
  1860.             }
  1861.                      }
  1862.             break; 
  1863.           case 'O':    // output path
  1864.             na++;     // sauter, dΘja traitΘ
  1865.             break;
  1866.           case 'P':    // proxy
  1867.             if ((na+1>=argc) || (argv[na+1][0]=='-')) {
  1868.               HTS_PANIC_PRINTF("Option P needs to be followed by a blank space, and a proxy proxy:port or user:id@proxy:port");
  1869.               printf("Example: -P proxy.myhost.com:8080\n");
  1870.               htsmain_free();
  1871.               return -1;
  1872.             } else {
  1873.               char* a;
  1874.               na++;
  1875.               httrack.proxy.active=1;
  1876.               // Rechercher MAIS en partant de la fin α cause de user:pass@proxy:port
  1877.               a = argv[na] + strlen(argv[na]) -1;
  1878.               // a=strstr(argv[na],":");  // port
  1879.               while( (a > argv[na]) && (*a != ':') && (*a != '@') ) a--;
  1880.               if (*a == ':') {  // un port est prΘsent, <proxy>:port
  1881.                 sscanf(a+1,"%d",&httrack.proxy.port);
  1882.                 httrack.proxy.name[0]='\0';
  1883.                 strncatbuff(httrack.proxy.name,argv[na],(int) (a - argv[na]));
  1884.               } else {  // <proxy>
  1885.                 httrack.proxy.port=8080;
  1886.                 strcpybuff(httrack.proxy.name,argv[na]);
  1887.               }
  1888.             }
  1889.             break;
  1890.           case 'F':    // user-agent field
  1891.             if ((na+1>=argc) || (argv[na+1][0]=='-')) {
  1892.               HTS_PANIC_PRINTF("Option F needs to be followed by a blank space, and a user-agent name");
  1893.               printf("Example: -F \"my_user_agent/1.0\"\n");
  1894.               htsmain_free();
  1895.               return -1;
  1896.             } else{
  1897.               na++;
  1898.               if (strlen(argv[na])>=126) {
  1899.                 HTS_PANIC_PRINTF("User-agent length too long");
  1900.                 htsmain_free();
  1901.                 return -1;
  1902.               }
  1903.               strcpybuff(httrack.user_agent,argv[na]);
  1904.               if (strnotempty(httrack.user_agent))
  1905.                 httrack.user_agent_send=1;
  1906.               else
  1907.                 httrack.user_agent_send=0;    // -F "" dΘsactive l'option
  1908.             }
  1909.             break;
  1910.             //
  1911.           case 'V':    // execute command
  1912.             if ((na+1>=argc) || (argv[na+1][0]=='-')) {
  1913.               HTS_PANIC_PRINTF("Option V needs to be followed by a system-command string");
  1914.               printf("Example: -V \"tar uvf some.tar \\$0\"\n");
  1915.               htsmain_free();
  1916.               return -1;
  1917.             } else{
  1918.               na++;
  1919.               if (strlen(argv[na])>=2048) {
  1920.                 HTS_PANIC_PRINTF("System-command length too long");
  1921.                 htsmain_free();
  1922.                 return -1;
  1923.               }
  1924.               strcpybuff(httrack.sys_com,argv[na]);
  1925.               if (strnotempty(httrack.sys_com))
  1926.                 httrack.sys_com_exec=1;
  1927.               else
  1928.                 httrack.sys_com_exec=0;    // -V "" dΘsactive l'option
  1929.             }
  1930.             break;
  1931.             //
  1932.           default: {
  1933.             char s[HTS_CDLMAXSIZE];
  1934.             sprintf(s,"invalid option %c\n",*com);
  1935.             HTS_PANIC_PRINTF(s);
  1936.             htsmain_free();
  1937.             return -1;
  1938.                    }
  1939.             break;
  1940.           }  // switch
  1941.           com++;    
  1942.         }  // while
  1943.         
  1944.       }  else {  // URL/filters
  1945.         char BIGSTK tempo[1024];       
  1946.         if (strnotempty(url)) strcatbuff(url," ");  // espace de sΘparation
  1947.         strcpybuff(tempo,unescape_http_unharm(argv[na],1));
  1948.         escape_spc_url(tempo);
  1949.         strcatbuff(url,tempo);
  1950.       }  // if argv=- etc. 
  1951.       
  1952.     }  // for
  1953.   }
  1954.   
  1955. #if BDEBUG==3  
  1956.   printf("URLs/filters=%s\n",url);
  1957. #endif
  1958.  
  1959. #if DEBUG_STEPS
  1960.   printf("Analyzing parameters done\n");
  1961. #endif
  1962.  
  1963.  
  1964. #if HTS_WIN
  1965. #else
  1966. #ifndef HTS_DO_NOT_USE_UID
  1967.   /* Chroot - xxc */
  1968.   if (switch_chroot) {
  1969.     uid_t userid=getuid();
  1970.     //struct passwd* userdef=getpwuid(userid);
  1971.     //if (userdef) {
  1972.     if (!userid) {
  1973.       //if (strcmp(userdef->pw_name,"root")==0) {
  1974.       char BIGSTK rpath[1024];
  1975.       //printf("html=%s log=%s\n",httrack.path_html,httrack.path_log);    // xxc
  1976.       if ((httrack.path_html[0]) && (httrack.path_log[0])) {
  1977.         char *a=httrack.path_html,*b=httrack.path_log,*c=NULL,*d=NULL;
  1978.         c=a; d=b;
  1979.         while ((*a) && (*a == *b)) {
  1980.           if (*a=='/') { c=a; d=b; }
  1981.           a++;
  1982.           b++;
  1983.         }
  1984.  
  1985.         rpath[0]='\0';
  1986.         if (c != httrack.path_html) {
  1987.           if (httrack.path_html[0]!='/')
  1988.             strcatbuff(rpath,"./");
  1989.           strncatbuff(rpath,httrack.path_html,(int) (c - httrack.path_html));
  1990.         }
  1991.         {
  1992.           char BIGSTK tmp[1024];
  1993.           strcpybuff(tmp,c); strcpybuff(httrack.path_html,tmp);
  1994.           strcpybuff(tmp,d); strcpybuff(httrack.path_log,tmp);
  1995.         }
  1996.       } else {
  1997.         strcpybuff(rpath,"./");
  1998.         strcpybuff(httrack.path_html,"/");
  1999.         strcpybuff(httrack.path_log,"/");
  2000.       }
  2001.       if (rpath[0]) {
  2002.         printf("[changing root path to %s (path_data=%s,path_log=%s)]\n",rpath,httrack.path_html,httrack.path_log);
  2003.         if (chroot(rpath)) {
  2004.           printf("ERROR! Can not chroot to %s!\n",rpath);
  2005.           return -1;
  2006.         }
  2007.         if (chdir("/")) {     /* new root */
  2008.           printf("ERROR! Can not chdir to %s!\n",rpath);
  2009.           return -1;
  2010.         }
  2011.       } else
  2012.         printf("WARNING: chroot not possible with these paths\n");
  2013.     }
  2014.     //}
  2015.   }
  2016.  
  2017.   /* Setuid */
  2018.   if (switch_uid>=0) {
  2019.     printf("[setting user/group to %d/%d]\n",switch_uid,switch_gid);
  2020.     if (setgid(switch_gid))
  2021.       printf("WARNING! Can not setgid to %d!\n",switch_gid);
  2022.     if (setuid(switch_uid))
  2023.       printf("WARNING! Can not setuid to %d!\n",switch_uid);
  2024.   }
  2025.  
  2026.   /* Final check */
  2027.   {
  2028.     uid_t userid=getuid();
  2029.     if (!userid) {              /* running as r00t */
  2030.       printf("WARNING! You are running this program as root!\n");
  2031.       printf("It might be a good idea to use the -%%U option to change the userid:\n");
  2032.       printf("Example: -%%U smith\n\n");
  2033.     }
  2034.   }
  2035. #endif
  2036. #endif
  2037.   
  2038.   //printf("WARNING! This is *only* a beta-release of HTTrack\n");
  2039.   io_flush;
  2040.   
  2041. #if DEBUG_STEPS
  2042.   printf("Cache & log settings\n");
  2043. #endif
  2044.   
  2045.   // on utilise le cache..
  2046.   // en cas de prΘsence des deux versions, garder la version la plus avancΘe,
  2047.   // cad la version contenant le plus de fichiers  
  2048.   if (httrack.cache) {
  2049.     if (fexist(fconcat(httrack.path_log,"hts-in_progress.lock"))) {   // problemes..
  2050.       if ( fexist(fconcat(httrack.path_log,"hts-cache/new.dat")) ) { 
  2051.         if ( fexist(fconcat(httrack.path_log,"hts-cache/old.zip")) ) {
  2052.           if (fsize(fconcat(httrack.path_log,"hts-cache/new.zip"))<32768) {
  2053.             if (fsize(fconcat(httrack.path_log,"hts-cache/old.zip"))>65536) {
  2054.               if (fsize(fconcat(httrack.path_log,"hts-cache/old.zip")) > fsize(fconcat(httrack.path_log,"hts-cache/new.zip"))) {
  2055.                 remove(fconcat(httrack.path_log,"hts-cache/new.zip"));
  2056.                 rename(fconcat(httrack.path_log,"hts-cache/old.zip"), fconcat(httrack.path_log,"hts-cache/new.zip"));
  2057.               }
  2058.             }
  2059.           }
  2060.         }
  2061.       }
  2062.       else if (fexist(fconcat(httrack.path_log,"hts-cache/new.dat")) && fexist(fconcat(httrack.path_log,"hts-cache/new.ndx"))) { 
  2063.         if (fexist(fconcat(httrack.path_log,"hts-cache/old.dat")) && fexist(fconcat(httrack.path_log,"hts-cache/old.ndx"))) {
  2064.           // switcher si new<32Ko et old>65Ko (tailles arbitraires) ?
  2065.           // ce cas est peut Ωtre une erreur ou un crash d'un miroir ancien, prendre
  2066.           // alors l'ancien cache
  2067.           if (fsize(fconcat(httrack.path_log,"hts-cache/new.dat"))<32768) {
  2068.             if (fsize(fconcat(httrack.path_log,"hts-cache/old.dat"))>65536) {
  2069.               if (fsize(fconcat(httrack.path_log,"hts-cache/old.dat")) > fsize(fconcat(httrack.path_log,"hts-cache/new.dat"))) {
  2070.                 remove(fconcat(httrack.path_log,"hts-cache/new.dat"));
  2071.                 remove(fconcat(httrack.path_log,"hts-cache/new.ndx"));
  2072.                 rename(fconcat(httrack.path_log,"hts-cache/old.dat"),fconcat(httrack.path_log,"hts-cache/new.dat"));
  2073.                 rename(fconcat(httrack.path_log,"hts-cache/old.ndx"),fconcat(httrack.path_log,"hts-cache/new.ndx"));  
  2074.                 //} else {  // ne rien faire
  2075.                 //  remove("hts-cache/old.dat");
  2076.                 //  remove("hts-cache/old.ndx");
  2077.               }
  2078.             }
  2079.           }
  2080.         }
  2081.       }
  2082.     }
  2083.   }
  2084.  
  2085.   // DΘbuggage des en tΩtes
  2086.   if (_DEBUG_HEAD) {
  2087.     ioinfo=fopen(fconcat(httrack.path_log,"hts-ioinfo.txt"),"wb");
  2088.   }
  2089.   
  2090.   {
  2091.     char n_lock[256];
  2092.     // on peut pas avoir un affichage ET un fichier log
  2093.     // ca sera pour la version 2
  2094.     if (httrack_logmode==1) {
  2095.       httrack.log=stdout;
  2096.       httrack.errlog=stderr;
  2097.     } else if (httrack_logmode>=2) {
  2098.       // deux fichiers log
  2099.       structcheck(httrack.path_log);
  2100.       if (fexist(fconcat(httrack.path_log,"hts-log.txt")))
  2101.         remove(fconcat(httrack.path_log,"hts-log.txt"));
  2102.       if (fexist(fconcat(httrack.path_log,"hts-err.txt")))
  2103.         remove(fconcat(httrack.path_log,"hts-err.txt"));
  2104.  
  2105.       /* Check FS directory structure created */
  2106.       structcheck(httrack.path_log);
  2107.  
  2108.       httrack.log=fopen(fconcat(httrack.path_log,"hts-log.txt"),"w");
  2109.       if (httrack_logmode==2)
  2110.         httrack.errlog=fopen(fconcat(httrack.path_log,"hts-err.txt"),"w");
  2111.       else
  2112.         httrack.errlog=httrack.log;
  2113.       if (httrack.log==NULL) {
  2114.         char s[HTS_CDLMAXSIZE];
  2115.         sprintf(s,"Unable to create log file %s",fconcat(httrack.path_log,"hts-log.txt"));
  2116.         HTS_PANIC_PRINTF(s);
  2117.         htsmain_free();
  2118.         return -1;
  2119.       } else if (httrack.errlog==NULL) {
  2120.         char s[HTS_CDLMAXSIZE];
  2121.         sprintf(s,"Unable to create log file %s",fconcat(httrack.path_log,"hts-err.txt"));
  2122.         HTS_PANIC_PRINTF(s);
  2123.         htsmain_free();
  2124.         return -1;
  2125.       }
  2126.  
  2127.     } else {
  2128.       httrack.log=NULL;
  2129.       httrack.errlog=NULL;
  2130.     }
  2131.     
  2132.     // un petit lock-file pour indiquer un miroir en cours, ainsi qu'un Θventuel fichier log
  2133.     {
  2134.       FILE* fp=NULL;
  2135.       //int n=0;
  2136.       char t[256];
  2137.       time_local_rfc822(t);    // faut bien que ca serve quelque part l'heure RFC1945 arf'
  2138.       
  2139.       /* readme for information purpose */
  2140.       {
  2141.         FILE* fp=fopen(fconcat(httrack.path_log,"hts-cache/readme.txt"),"wb");
  2142.         if (fp) {
  2143.           fprintf(fp,"What's in this folder?"LF);
  2144.           fprintf(fp,""LF);
  2145.           fprintf(fp,"This folder (hts-cache) has been generated by WinHTTrack "HTTRACK_VERSION"%s"LF, WHAT_is_available);
  2146.           fprintf(fp,"and is used for updating this website."LF);
  2147.           fprintf(fp,"(The HTML website structure is stored here to allow fast updates)"LF""LF);
  2148.           fprintf(fp,"DO NOT delete this folder unless you do not want to update the mirror in the future!!"LF);
  2149.           fprintf(fp,"(you can safely delete old.zip and old.lst files, however)"LF);
  2150.           fprintf(fp,""LF);
  2151.           fprintf(fp,HTS_LOG_SECURITY_WARNING);
  2152.           fclose(fp);
  2153.         }
  2154.       }
  2155.  
  2156.       sprintf(n_lock,fconcat(httrack.path_log,"hts-in_progress.lock"));
  2157.       //sprintf(n_lock,fconcat(httrack.path_log,"hts-in_progress.lock"),n);
  2158.       /*do {
  2159.         if (!n)
  2160.           sprintf(n_lock,fconcat(httrack.path_log,"hts-in_progress.lock"),n);
  2161.         else
  2162.           sprintf(n_lock,fconcat(httrack.path_log,"hts-in_progress%d.lock"),n);
  2163.         n++;
  2164.       } while((fexist(n_lock)) && httrack.quiet);      
  2165.       if (fexist(n_lock)) {
  2166.         if (!recuperer) {
  2167.           remove(n_lock);
  2168.         }
  2169.       }*/
  2170.  
  2171.       // vΘrifier existence de la structure
  2172.       structcheck(fconcat(httrack.path_html, "/"));
  2173.       structcheck(fconcat(httrack.path_log, "/"));
  2174.      
  2175.       // reprise/update
  2176.       if (httrack.cache) {
  2177.         FILE* fp;
  2178.         int i;
  2179. #if HTS_WIN
  2180.         mkdir(fconcat(httrack.path_log,"hts-cache"));
  2181. #else
  2182.         mkdir(fconcat(httrack.path_log,"hts-cache"),HTS_PROTECT_FOLDER);
  2183. #endif
  2184.         fp=fopen(fconcat(httrack.path_log,"hts-cache/doit.log"),"wb");
  2185.         if (fp) {
  2186.           for(i=0+1;i<argc;i++) {
  2187.             if ( ((strchr(argv[i],' ')!=NULL) || (strchr(argv[i],'"')!=NULL) || (strchr(argv[i],'\\')!=NULL)) && (argv[i][0]!='"')  ) {
  2188.               int j;
  2189.               fprintf(fp,"\"");
  2190.               for(j=0;j<(int) strlen(argv[i]);j++) {
  2191.                 if (argv[i][j]==34)
  2192.                   fprintf(fp,"\\\"");
  2193.                 else if (argv[i][j]=='\\')
  2194.                   fprintf(fp,"\\\\");
  2195.                 else
  2196.                   fprintf(fp,"%c",argv[i][j]);
  2197.               }
  2198.               fprintf(fp,"\"");
  2199.             } else if (strnotempty(argv[i])==0) {   // ""
  2200.               fprintf(fp,"\"\"");
  2201.             } else {   // non critique
  2202.               fprintf(fp,"%s",argv[i]);
  2203.             }
  2204.             if (i<argc-1)
  2205.               fprintf(fp," ");
  2206.           }
  2207.           fprintf(fp,LF);
  2208.           fprintf(fp,"File generated automatically on %s, do NOT edit"LF,t);
  2209.           fprintf(fp,LF);
  2210.           fprintf(fp,"To update a mirror, just launch httrack without any parameters"LF);
  2211.           fprintf(fp,"The existing cache will be used (and modified)"LF);
  2212.           fprintf(fp,"To have other options, retype all parameters and launch HTTrack"LF);
  2213.           fprintf(fp,"To continue an interrupted mirror, just launch httrack without any parameters"LF);
  2214.           fprintf(fp,LF);
  2215.           fclose(fp); fp=NULL;
  2216.         //} else if (httrack.debug>1) {
  2217.         //  printf("! FileOpen error, \"%s\"\n",strerror(errno));
  2218.         }
  2219.       }
  2220.       
  2221.       // petit message dans le lock
  2222.       if ( (fp=fopen(n_lock,"wb"))!=NULL) {
  2223.         int i;
  2224.         fprintf(fp,"Mirror in progress since %s .. please wait!"LF,t);
  2225.         for(i=0;i<argc;i++) {
  2226.           if (strchr(argv[i],' ')==NULL)
  2227.             fprintf(fp,"%s ",argv[i]);
  2228.           else    // entre ""
  2229.             fprintf(fp,"\"%s\" ",argv[i]);
  2230.         }
  2231.         fprintf(fp,LF);
  2232.         fprintf(fp, "To pause the engine: create an empty file named 'hts-stop.lock'"LF);
  2233.         fclose(fp); fp=NULL;
  2234.       }
  2235.       
  2236.       // fichier log        
  2237.       if (httrack.log)     {
  2238.         int i;
  2239.         fprintf(httrack.log,"HTTrack"HTTRACK_VERSION"%s launched on %s at %s"LF, 
  2240.           WHAT_is_available,
  2241.           t, url);
  2242.         fprintf(httrack.log,"(");
  2243.         for(i=0;i<argc;i++) {
  2244.           if ((strchr(argv[i],' ')==NULL) || (strchr(argv[i],'\"')))
  2245.             fprintf(httrack.log,"%s ",argv[i]);
  2246.           else    // entre "" (si espace(s) et pas dΘja de ")
  2247.             fprintf(httrack.log,"\"%s\" ",argv[i]);
  2248.         }
  2249.         fprintf(httrack.log,")"LF);
  2250.         fprintf(httrack.log,LF);
  2251.         fprintf(httrack.log,"Information, Warnings and Errors reported for this mirror:"LF);
  2252.         fprintf(httrack.log,HTS_LOG_SECURITY_WARNING );
  2253.         fprintf(httrack.log,LF);
  2254.       }
  2255.  
  2256.       if (httrack_logmode) {
  2257.         printf("Mirror launched on %s by HTTrack Website Copier/"HTTRACK_VERSION"%s "HTTRACK_AFF_AUTHORS""LF,t,WHAT_is_available);
  2258.         if (httrack.wizard==0) {
  2259.           printf("mirroring %s with %d levels, %d sockets,t=%d,s=%d,logm=%d,lnk=%d,mdg=%d\n",url,httrack.depth,httrack.maxsoc,httrack.travel,httrack.seeker,httrack_logmode,httrack.urlmode,httrack.getmode);
  2260.         } else {    // the magic wizard
  2261.           printf("mirroring %s with the wizard help..\n",url);
  2262.         }
  2263.       }
  2264.     }
  2265.     
  2266.     io_flush;
  2267.  
  2268.   /* Info for wrappers */
  2269.   if ( (httrack.debug>0) && (httrack.log!=NULL) ) {
  2270.     fspc(httrack.log,"info"); fprintf(httrack.log,"engine: init"LF);
  2271.   }
  2272. #if HTS_ANALYSTE
  2273.   hts_htmlcheck_init();
  2274.   set_wrappers();   // init() is allowed to set other wrappers
  2275. #endif
  2276.  
  2277.   // dΘtourner SIGHUP etc.
  2278. #if HTS_WIN
  2279. #ifndef  _WIN32_WCE
  2280.   signal( SIGINT  , sig_ask    );   // ^C
  2281.   signal( SIGTERM , sig_finish );   // kill <process>
  2282. #endif
  2283. #else
  2284.   signal( SIGHUP  , sig_back   );   // close window
  2285.   signal( SIGTSTP , sig_back   );   // ^Z
  2286.   signal( SIGTERM , sig_finish );   // kill <process>
  2287.   signal( SIGINT  , sig_ask    );   // ^C
  2288.   signal( SIGPIPE , sig_brpipe );   // broken pipe (write into non-opened socket)
  2289. /*
  2290. deprecated - see SIGCHLD
  2291. #ifndef HTS_DO_NOT_SIGCLD
  2292.   signal( SIGCLD  , sig_ignore );   // child change status
  2293. #endif
  2294. */
  2295.   signal( SIGCHLD , sig_ignore );   // child change status
  2296. #endif
  2297. #if DEBUG_STEPS
  2298.   printf("Launching the mirror\n");
  2299. #endif
  2300.   
  2301.  
  2302.     // Lancement du miroir
  2303.     // ------------------------------------------------------------
  2304.     if (httpmirror(url, &httrack)==0) {
  2305.       printf("Error during operation (see log file), site has not been successfully mirrored\n");
  2306.     } else {
  2307.       if  (httrack.shell) {
  2308.         HTT_REQUEST_START;
  2309.         HT_PRINT("TRANSFER DONE"LF);
  2310.         HTT_REQUEST_END
  2311.       } else {
  2312.         printf("Done.\n");
  2313.       }
  2314.     }
  2315.     // ------------------------------------------------------------
  2316.  
  2317.     //
  2318.     // Build top index
  2319.     if (httrack.dir_topindex) {
  2320.       char BIGSTK rpath[1024*2];
  2321.       char* a;
  2322.       strcpybuff(rpath,httrack.path_html);
  2323.       if (rpath[0]) {
  2324.         if (rpath[strlen(rpath)-1]=='/')
  2325.           rpath[strlen(rpath)-1]='\0';
  2326.       }
  2327.       a=strrchr(rpath,'/');
  2328.       if (a) {
  2329.         *a='\0';
  2330.         hts_buildtopindex(&httrack,rpath,httrack.path_bin);
  2331.         if (httrack.log) {
  2332.           fspc(httrack.log,"info"); fprintf(httrack.log,"Top index rebuilt (done)"LF);
  2333.         }
  2334.       }
  2335.     }
  2336.  
  2337.     if (exit_xh ==1) {
  2338.       if (httrack.log) {
  2339.         fprintf(httrack.log,"* * MIRROR ABORTED! * *\nThe current temporary cache is required for any update operation and only contains data downloaded during the present aborted session.\nThe former cache might contain more complete information; if you do not want to lose that information, you have to restore it and delete the current cache.\nThis can easily be done here by erasing the hts-cache/new.* files]\n");
  2340.       }
  2341.     }
  2342.  
  2343.     /* Info for wrappers */
  2344.     if ( (httrack.debug>0) && (httrack.log!=NULL) ) {
  2345.       fspc(httrack.log,"info"); fprintf(httrack.log,"engine: free"LF);
  2346.     }
  2347. #if HTS_ANALYSTE
  2348.     hts_htmlcheck_uninit();
  2349. #endif
  2350.  
  2351.     if (httrack_logmode!=1) {
  2352.       if (httrack.errlog == httrack.log) httrack.errlog=NULL;
  2353.       if (httrack.log) { fclose(httrack.log); httrack.log=NULL; }
  2354.       if (httrack.errlog) { fclose(httrack.errlog); httrack.errlog=NULL; }
  2355.     }  
  2356.  
  2357.     // DΘbuggage des en tΩtes
  2358.     if (_DEBUG_HEAD) {
  2359.       if (ioinfo) {
  2360.         fclose(ioinfo);
  2361.       }
  2362.     }
  2363.  
  2364.     // supprimer lock
  2365.     remove(n_lock);
  2366.   }
  2367.  
  2368.   if (x_argvblk)
  2369.     freet(x_argvblk);
  2370.   if (x_argv)
  2371.     freet(x_argv);
  2372.  
  2373. #if HTS_WIN
  2374. #if HTS_ANALYSTE!=2
  2375. //  WSACleanup();    // ** non en cas de thread tjs prΘsent!..
  2376. #endif
  2377. #endif
  2378. #ifdef HTS_TRACE_MALLOC
  2379.   hts_freeall();
  2380. #endif
  2381.  
  2382.   printf("Thanks for using HTTrack!\n");
  2383.   io_flush;
  2384.   htsmain_free();
  2385.   return 0;    // OK
  2386. }
  2387.  
  2388.  
  2389. // main() subroutines
  2390.  
  2391. // vΘrifier chemin path
  2392. int check_path(char* s,char* defaultname) {
  2393.   int i;
  2394.   int return_value=0;
  2395.  
  2396.   // Replace name: ~/mywebsites/# -> /home/foo/mywebsites/#
  2397.   expand_home(s);
  2398.   for(i=0;i<(int) strlen(s);i++)    // conversion \ -> /
  2399.     if (s[i]=='\\')
  2400.       s[i]='/';
  2401.   
  2402.   // remove ending /
  2403.   if (strnotempty(s))
  2404.   if (s[strlen(s)-1]=='/')
  2405.     s[strlen(s)-1]='\0';
  2406.  
  2407.    // Replace name: /home/foo/mywebsites/# -> /home/foo/mywebsites/wonderfulsite
  2408.   if (strnotempty(s)) {
  2409.     if (s[(i=strlen(s))-1]=='#') {
  2410.       if (strnotempty((defaultname?defaultname:""))) {
  2411.         char BIGSTK tempo[HTS_URLMAXSIZE*2];
  2412.         char* a=strchr(defaultname,'#');      // we never know..
  2413.         if (a) *a='\0';
  2414.         tempo[0]='\0';
  2415.         strncatbuff(tempo,s,i-1);
  2416.         strcatbuff(tempo,defaultname);
  2417.         strcpybuff(s,tempo);
  2418.       } else
  2419.         s[0]='\0';            // Clear path (no name/default url given)
  2420.       return_value=1;     // expanded
  2421.     }
  2422.   }
  2423.  
  2424.   // ending /
  2425.   if (strnotempty(s))
  2426.   if (s[strlen(s)-1]!='/')    // ajouter slash α la fin
  2427.     strcatbuff(s,"/");
  2428.  
  2429.   return return_value;
  2430. }
  2431.  
  2432. // dΘtermine si l'argument est une option
  2433. int cmdl_opt(char* s) {
  2434.   if (s[0]=='-') {  // c'est peut Ωtre une option
  2435.     if (strchr(s,'.')!=NULL && strchr(s,'%')==NULL)
  2436.       return 0;    // sans doute un -www.truc.fr (note: -www n'est pas compris)
  2437.     else if (strchr(s,'/')!=NULL)
  2438.       return 0;    // idem, -*cgi-bin/
  2439.     else if (strchr(s,'*')!=NULL)
  2440.       return 0;    // joker, idem
  2441.     else
  2442.       return 1;
  2443.   } else return 0;
  2444. }
  2445.  
  2446.